1

I want to extends the Blog plugin by changing the property $allowedSortingOptions and also the scopeListFrontEnd() scope.

The property defines the sorting options available and I want to add some other options. Original value from Blog Post.php :

    public static $allowedSortingOptions = [
        'title asc'         => 'rainlab.blog::lang.sorting.title_asc',
        'title desc'        => 'rainlab.blog::lang.sorting.title_desc',
        'created_at asc '   => 'rainlab.blog::lang.sorting.created_asc',
        'created_at desc'   => 'rainlab.blog::lang.sorting.created_desc',
        'updated_at asc'    => 'rainlab.blog::lang.sorting.updated_asc',
        'updated_at desc'   => 'rainlab.blog::lang.sorting.updated_desc',
        'published_at asc'  => 'rainlab.blog::lang.sorting.published_asc',
        'published_at desc' => 'rainlab.blog::lang.sorting.published_desc',
        'random'            => 'rainlab.blog::lang.sorting.random'
    ];

I want also to extend the scope logic. Original method from Blog Post.php :

    public function scopeListFrontEnd($query, $options)
    {
      // Code ...
      return $query->paginate($perPage, $page);
    }

In my plugin I tried several ways to do this inside the Plugin.php file.

private function extendModel()
    {
        // Code ...

        PostModel::extend(function ($model) {
            // Code ...

            // This does not work
            // I want to override $setAllowedSortingOptions here and add other options
            $model::$allowedSortingOptions = [
                // My Options
            ];

            // This does not work either
            $model->addDynamicProperty('allowedSortingOptions', $MyOptions);

            // I want to retrieve the result query of the scope ListFrontEnd to change it
            Event::listen('backend.filter.extendQuery', function($filterWidget, $query, $scope) {
                if ($scope->scopeName == 'listFrontEnd') {
                    // Change the $query order here
                }
            });
        });
    }
  • So, how can I override a Class property ?
  • And how can I extends/override a Scope or Method ?

Thanks.

Zied Hf
  • 491
  • 3
  • 10
  • 30
  • It is possible to override `$allowedSortingOptions`. But sadly its not possible to override `scopeListFrontEnd` method neither extend it as code do not provide any events so. – Hardik Satasiya Oct 10 '19 at 06:49
  • Hi @Hardik Satasiya, you mention that overriding the sorting options does work. Would this mean that we could get the sorting of blog posts based on a custom database field up and running? If so what would be the right way to extend $allowedSortingOptions in this case? Thanks in advance, your answers are always very useful – chocolata Nov 19 '20 at 23:18

0 Answers0