This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.

planetadeleste
planetadeleste

I know it is possible to extend the filter query. But I don't know if is possible to add new fields to filters.

I have a plugin that publish contents on Facebook. It extend the controller, adding news fields. But I want to add filters by published/unpublished records, on the extended plugin list controller.

It can be done?

Thanks

planetadeleste
planetadeleste

Ok, I figured how to do it. I share my implementation of how to extend filters.

Model extension

In my plugin, I extend plugins dynamically, based on backend configuration.

    // Plugin.php
    public function boot()
    {
        // ... 

        // EXTEND MODEL
        $model = new $modelClass;
        $model::extend(
            function ($modelExt) {
                // Add the relation
                $modelExt->morphMany['sharehistory'] = [
                    'PlanetaDelEste\SocialPublish\Models\Post',
                    'name' => 'shareable',
                ];

                // Add the scope isShared()
                $modelExt->addDynamicMethod(
                    'scopeIsShared',
                    function ($query) {
                        /** @var \October\Rain\Database\Builder $query */
                        return $query->has('sharehistory');
                    }
                );

            }
        );

        // EXTEND LIST COLUMNS
        // vars $integration and $model are used in my case with dynamic integration
        Event::listen(
            'backend.list.extendColumns',
            function ($widget) use ($integration, $models) {
                /** @var Backend\Widgets\Form $widget */
                $widgetController = $widget->getController();
                $widgetControllerClass = get_class($widgetController);

                // Only If has integrated
                if (!is_array($integration) || empty($integration)) {
                    return;
                }

                // Only for the integrated controller
                if (!in_array($widgetControllerClass, $integration)) {
                    return;
                }

                // Only for the integrated model
                if (!in_array(get_class($widget->model), $models)) {
                    return;
                }

                // Add an extra history column
                $widget->addColumns(
                    [
                        'history' => [
                            'label'    => 'planetadeleste.socialpublish::lang.fields.published',
                            'type'     => 'partial',
                            'sortable' => false,
                            'path'     => '~/plugins/planetadeleste/socialpublish/partials/_history.htm',
                        ],
                    ]
                );

            }
        );

        // EXTEND LIST FILTER COLUMNS
        // Assumed the plugin has the filters configured
        // vars $integration and $model are used in my case with dynamic integration
        Event::listen(
            'backend.filter.extendScopes',
            function ($widget) use ($integration, $models) {
                /** @var Backend\Widgets\Filter $widget */
                $widgetController = $widget->getController();
                $widgetControllerClass = get_class($widgetController);

                // Only If has integrated
                if (!is_array($integration) || empty($integration)) {
                    return;
                }

                // Only for the integrated controller
                if (!in_array($widgetControllerClass, $integration)) {
                    return;
                }

                // Add an extra filter column scope
                // See I use the isShared scope, added in $model->addDynamicMethod()
                $widget->addScopes(
                    [
                        'shared' => [
                            'label' => 'planetadeleste.socialpublish::lang.fields.published',
                            'type'  => 'checkbox',
                            'scope' => 'isShared',
                        ],
                    ]
                );

            }
        );
    }

Final result (screen in Spanish)

Final Result

jfo
jfo

Great! Thanks for sharing! ;-)

leotrooper14689
leotrooper14689

I got: Undefined variable: modelClass

Can you help me with this?

salma el hajraoui

1-5 of 5

You cannot edit posts or make replies: the forum has moved to talk.octobercms.com.