Search code examples
phpsymfonysonata-adminsymfony-sonata

Predefined parameters for Sonata list search


I need to set several predefined options for showing list of entities.

Sonata code:

class SubscriptionAdmin extends Admin {
    protected function configureDatagridFilters(DatagridMapper $datagridMapper)
    {
        $datagridMapper
            ->add('created_at')
            ->add('status', 'doctrine_orm_choice', [], 'choice', ['choices' = ['active'=>'ecmo.enum.user.active', 'inactive'=>'ecmo.enum.user.active']
        ;
}

How i can set, that, by default, sonata show only active users, sorted by created_at ?


Solution

  • You could do this, setting the default values for the $datagridValues variable as below

    class SubscriptionAdmin extends Admin
    {   
    
       /**
        * Default Datagrid values
        *
        * @var array
        */
       protected $datagridValues = array (
               'status' => array ('type' => 1, 'value' => 1), // field status with value 1
               '_page' => 1, // Display the first page (default = 1)
               '_sort_order' => 'ASC', // Descendant ordering (default = 'ASC')
               '_sort_by' => 'name' // name of the ordered field (default = the model id field, if any)
          // the '_sort_by' key can be of the form 'mySubModel.mySubSubModel.myField'.
       );
    }
    

    Where 'status' should be substituted by the field that you want and you should specify the desired value, and 'type' value corresponds with the following:

    1: =
    2: >=
    3: >
    4: <=
    5: <
    

    That's to filter an integer field. In my case, I've used this without specifying type, and the value 1 is set as default.