简体   繁体   中英

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 .

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM