Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony2 - Give a default filter in a list of elements of Sonata Admin

I have a list of elements of type Vehicle and I show these elements with Sonata Admin. I allow to filter these elements by the "status" field, but I want that, when the list is showed, only the active vehicles are showed, and if somebody wants to see the inactive vehicles, uses the filter and select the inactive status. I would like to know if somebody Knows the way to apply filters by default for a list of elements using Sonata Admin.

Here is my code:

public function configureListFields(ListMapper $listMapper)
{
    $listMapper
        ->addIdentifier('name')
        ->add('status')
    ;
}

protected function configureDatagridFilters(DatagridMapper $mapper)
 {
     $mapper
         ->add('name')
         ->add('status')
     ;
 }

Is there any option that can be added to the status field in configureDatagridFilters() to achieve this goal? Other options?

Thanks in advance.

like image 777
Airam Avatar asked Apr 25 '13 11:04

Airam


4 Answers

You have to override $datagridValues property as following (for status > 0 if it's an integer) :

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

source: Configure the default page and ordering in the list view

like image 50
AlterPHP Avatar answered Nov 12 '22 15:11

AlterPHP


You can also use this method

    public function getFilterParameters()
    {
        $this->datagridValues = array_merge(
            array(
                'status' => array (
                    'type' => 1,
                    'value' => 0
                ),
                // exemple with date range
                'updatedAt' => array(
                    'type' => 1,
                    'value' => array(
                        'start' => array(
                            'day' => date('j'),
                            'month' => date('m'),
                            'year' => date('Y')
                            ),
                        'end' => array(
                            'day' => date('j'),
                            'month' => date('m'),
                            'year' => date('Y')
                            )
                        ),
                    )
                ),
            $this->datagridValues
            );

        return parent::getFilterParameters();
    }
like image 31
Quiche Avatar answered Nov 12 '22 17:11

Quiche


Using both above suggested approaches will break the filters "reset" behaviour since we are always forcing the filter to filter by a default value. To me, i think the best approach is to use the getFilterParameters function (since we can add logic in there instead of statically add the value) and check if the user clicked the "Reset button"

/**
 * {@inheritdoc}
 */
public function getFilterParameters()
{
    // build the values array
    if ($this->hasRequest()) {
        $reset = $this->request->query->get('filters') === 'reset';

        if (!$reset) {
            $this->datagridValues = array_merge(array(
                'status' => array (
                    'type' => 1,
                    'value' => 0
                ),
            ),
                $this->datagridValues
            );
        }
    }

    return parent::getFilterParameters();
}
like image 8
eduardo_conceicao Avatar answered Nov 12 '22 17:11

eduardo_conceicao


Another approach is to use createQuery and getPersistentParameters to enforce invisible filter. This approach is best to have fully customizable filters. See my articles here:

http://www.theodo.fr/blog/2016/09/sonata-for-symfony-hide-your-filters/

like image 1
vicdup Avatar answered Nov 12 '22 15:11

vicdup