sulu

How to define a default sorting in a Sulu admin list view


I have created a REST controller for a custom entity that should be administrated by the backend of the Sulu CMS. My implementation is analog to this example.

Now I want to define a default sorting at the list view. Currently the items are listed just like they are fetched from the database. Of course the user can sort it in the frontend.

But I want to order the list per default by the entities name attribute.


Solution

  • If you are using the listBuilder with fieldDescriptors you can do something like this:

    $fieldDescriptors = $this->fieldDescriptorFactory->getFieldDescriptors('your_list');
    
    // ....
    
    if (!$request->query->get('sortBy')) { // set default sorting when no sorting is given
        $listBuilder->sort($fieldDescriptors['created'], 'desc');
    }