Sonata for Symfony: Hide Your Filters!
Victor Duprez4 min read
How to enforce and hide filters on a Sonata’s list view
Sonata provides you with a robust and easy to implement interface administration for your Symfony projects.
We have been using and loving it on my projects for 19 weeks now. Every day we are amazed with the time we save thanks to Sonata.
However, there are some features that our client wants that are missing. One of them is the possibility to hide filters.
Imagine you want your users to see a pre-filtered Sonata list and do not display the filters.
In our case we receive applications. Each application has a status and a at certain point we have a button “View pending application”. This button of course is supposed to list only the “pending” applications and we don’t want the users to be able to change this filter.
This solution is really straight-forward and easy to implement, give us 5 minutes and 30 lines of code!
Note: this tutorial is written for Symfony 2.8 and Sonata 2.3. Let me know in the comments if you tested it on other configurations.
The easy solution : createQuery
When you search for a solution online you easily find examples of people using the createQuery function of Sonata:
In your admin class:
class ApplicationAdmin extends SonataAdmin
{
public function createQuery($context = 'list')
{
$query = parent::createQuery($context);
$rootAlias = $query->getRootAliases()[0];
$query
->andWhere($query->expr()->eq($rootAlias . '.status', ':statusPending'))
->setParameter('statusPending', Application::STATUS_PENDING)
;
return $query;
}
}
This works great but we had one major issue with that solution:
- The filter is always enforced and there is no way to display the same list without it.
The better solution : hiddenFilters
In our project we had to display the same list, sometimes with hidden filters enforced, sometimes not.
That’s why we went a step further and created a “HiddenFilters” mechanism.
- Use URL parameters to define which filters to set up
class ApplicationAdmin extends SonataAdmin
{
// This array will contain our filters, filters are an object with a unique key and a value
private $hiddenFilters;
// This function reads the filters from the URL
private function readHiddenFilters()
{
return $this->getRequest()->query->get('hiddenFilters');
}
// CreateQuery is called to generate the list before the admin list view is rendered
public function createQuery($context = 'list')
{
$query = parent::createQuery($context);
$rootAlias = $query->getRootAliases()[0];
// Make sure we have the last filters in memory
$this->hiddenFilters = $this->readHiddenFilters();
// Do any kind of check on those filters, here we check that "statusPendingFilter: true" was set
if (is_array($this->hiddenFilters) && array_key_exists('statusPendingFilter', $this->hiddenFilters) && $this->hiddenFilters['statusPendingFilter']) {
$query
->andWhere($query->expr()->eq($rootAlias . '.status', ':statusPending'))
->setParameter('statusPending', 'PENDING')
;
}
return $query;
}
}
Now, when you want to activate a hidden filter on your list, call it with the correct parameters in the URL:
<a href="{{ path('admin_application_list', {'hiddenFilters' : {'statusPendingFilter' : true}}) }}">
$this->router->generate('admin_application_list', array('hiddenFilters[statusPendingFilter]' => true));
- Save your hidden filters as persistent parameters
You now have great filters you can enable and disable at will. Last issue: when you edit an element and then come back to the list your filters have disappeared.
The fix for that is to use Sonata’s persistent parameters: it’s a really handy function that will had your filters to all the Sonata generated links.
Add that to your admin class:
public function getPersistentParameters()
{
$parameters = parent::getPersistentParameters();
if (!$this->getRequest()) {
return array();
}
return array_merge($parameters, array(
'hiddenFilters' => is_array($this->hiddenFilters) ? json_encode($this->hiddenFilters) : $this->hiddenFilters,
));
}
Notice that we had to JSON_encode our filters because persistent parameters can only be strings.
That means we are going to have to change our readHiddenFilters function to decode the JSON:
private function readHiddenFilters()
{
return is_array($this->getRequest()->query->get('hiddenFilters')) ?
$this->getRequest()->query->get('hiddenFilters') :
json_decode($this->getRequest()->query->get('hiddenFilters'), true);
}
That’s it! You have fully customizable and activable hidden filters!
The full code : 30 lines
Here is the full code of working hidden filters:
class ApplicationAdmin extends SonataAdmin
{
private $hiddenFilters;
public function __construct($code, $class, $baseControllerName)
{
parent::__construct($code, $class, $baseControllerName);
$this->hiddenFilters = $this->readHiddenFilters();
}
private function readHiddenFilters()
{
return is_array($this->getRequest()->query->get('hiddenFilters')) ?
$this->getRequest()->query->get('hiddenFilters') :
json_decode($this->getRequest()->query->get('hiddenFilters'), true);
}
public function getPersistentParameters()
{
$parameters = parent::getPersistentParameters();
if (!$this->getRequest()) {
return array();
}
return array_merge($parameters, array(
'hiddenFilters' => is_array($this->hiddenFilters) ? json_encode($this->hiddenFilters) : $this->hiddenFilters,
));
}
public function createQuery($context = 'list')
{
$query = parent::createQuery($context);
$rootAlias = $query->getRootAliases()[0];
if (array_key_exists('statusPendingFilter', $this->hiddenFilters) && $this->hiddenFilters['statusPendingFilter']) {
$query
->andWhere($query->expr()->eq($rootAlias . '.status', ':statusPending'))
->setParameter('statusPending', Application::STATUS_PENDING)
;
}
return $query;
}
}