phpsymfonysonata-adminsonatasymfony-sonata

Sonata default filter value


I have a property in my method configureDatagridFilters() called assignee. When the list view of my admin is first loaded, I'd like to set the value of this property to the current user.

I have tried:

public function getFilterParameters()
{
    $parameters = parent::getFilterParameters();

    $parameters['assignee'] = [
        'value' => $this->getUser(),
    ];

    return $parameters;
}

as well as an array_merge instead. None of them achieved what I'm after, it still just shows me the default/entire list.

I have tried adding a type, bit unclear as to what the type is as some examples I've seen are like EntityType::class and others just a number 3.


Solution

  • So I figured out how to set the filter by default to the current user.

    My filter is an EntityType::class with the class of User::class. In order for the code snippet above to work, you must set the value to the user ID and not the user object like so: 'value' => $this->getUser()->getId().

    So the full method would be:

    public function getFilterParameters()
    {
        $parameters = parent::getFilterParameters();
    
        $parameters['assignee'] = [
            'value' => $this->getUser()->getId(),
        ];
    
        return $parameters;
    }