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

jfo
jfo

Hi,

I'm using the awesome List Behavior and now I need to prevent a list from loading data by default.

  • The list should be empty, no records displayed, when the page loads
  • When the user enters search criteria in the search input then the records that match the search term will be displayed

Are there any settings to tell the list controller to not load data upfront? If overriding the controller index method is required, what's the best practice to follow to check if a search term has been entered and return and empty list?

Thanks!

jfo
jfo

Does anyone could point me to some direction here? Am I the first one who needed to display an empty list?

So far I've found in modules/backend/behaviors/ListController.php

public function makeList($definition = null)
{
 ...
            /*
             * Link the Search Widget to the List Widget
             */
            if ($searchWidget = $toolbarWidget->getSearchWidget()) {
                $searchWidget->bindEvent('search.submit', function () use ($widget, $searchWidget) {
                    $widget->setSearchTerm($searchWidget->getActiveTerm());  // here it's the search term to which I need access
                    return $widget->onRefresh();
                });

                // Find predefined search term
                $widget->setSearchTerm($searchWidget->getActiveTerm());
            }
 ...
}

I couldn't, however, find a way to get access to $searchWidget->getActiveTerm() from my controller. Any ideas?

Scott
Scott

You should be able to just override the makeList() method. From there you can do anything you need to... https://github.com/octobercms/library/blob/master/src/Extension/README.md

Last updated

jfo
jfo

Hi @Scott, thanks for replying. I'm probably missing some obvious piece of the equation as I can't get it to work. Here is the full implementation of my controller class:

class MyController extends Controller
{
    public $implement = [
        'Backend.Behaviors.FormController',
        'Backend.Behaviors.ListController'
    ];

    public $formConfig = 'config_form.yaml';
    public $listConfig = 'config_list.yaml';

    public function __construct()
    {
        parent::__construct();

        BackendMenu::setContext('My.Plugin', 'plugin', 'name');
    }

    public function makeList($definition = null)
    {
        dd("my overridden version of makeList"); // this line is not being executed
    }
}

As you can see the makeList method is being overridden but not being called. When loading the controller the original makeList method implemented on the ListController behavior is being triggered instead.

Any ideas why my overridden version of makeList is not being invoked?

jfo
jfo

Could anyone help me on that? Any ideas on what's wrong with my makeList method above? What's the right way to override a method defined in ListController?

Thanks!

jfo
jfo

I finally found out a way to prevent search from returning all records by listening to the listExtendQuery event. But this is still doesn't look like a great solution:

    public function listExtendQuery($query, $definition = null)
    {
        if (count($query->getBindings()) == 0) {
            // injects a where clause to prevent any records from being retrieved
            $query->where('id', '-999');
        }
    }

As this is the Best Practice forum, I'd be interested in hear better alternatives to achieve the same goal - if any exists.

Thanks!

Last updated

1-6 of 6

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