I would like to add a custom condition to the queries which are generated by Sonata Search feature. The problem is that i have 'status' column which should be set as "active". On the List View i do not have any problem because I am able to set:
protected $datagridValues = array (
'status' => array ('type' => 1, 'value' => Status::ACTIVE)
);
and then all queries check if the status field is set properly. But the problem is with global search. I can override SearchHandler and force desired behavior, but i can't change any files from vendor/ directory, so i have two questions.
SOLUTION:
I have figure out how can i inject my own SearchHandler. The following code is used for that: 1. Just edit your services.yml file and put something like that:
cmsbundle.search.handler:
class: XXX\CmsBundle\Search\SearchHandler
arguments:
- @sonata.admin.pool
sonata.admin.block.search_result:
class: XXX\CmsBundle\Search\AdminSearchBlockService
tags:
- { name: sonata.block }
arguments:
- sonata.admin.block.search_result
- @templating
- @sonata.admin.pool
- @cmsbundle.search.handler
Create the file "XXX\CmsBundle\Search\SearchHandler" and change implementation. It can be something like that:
foreach ($datagrid->getFilters() as $name => $filter) {
/** @var $filter FilterInterface */
if ($filter->getOption('global_search', false)) {
if ($filter->getName() !== 'status') {
$filter->setCondition(FilterInterface::CONDITION_OR);
$datagrid->setValue($name, null, $term);
} else {
$filter->setCondition(FilterInterface::CONDITION_AND);
$datagrid->setValue($name, null, 'active');
}
$found = true;
}
}
IMPORTANT
'status' field must be added to configureDatagridFilters method in Admin class.