Skip to content

Filter groups not applying correctly for customer search #24576

Closed
@mlambley

Description

@mlambley

Preconditions (*)

magento/product-community-edition 2.3.2

Steps to reproduce (*)

I'm trying to modify the customer filters. See <backend>/customer/index/index, click Filters, change a value and click Apply Filters. I'm trying to say "for this particular filter, also search for null values".

The documentation for \Magento\Framework\Api\Search\FilterGroup says that it "Groups two or more filters together using a logical OR" so in theory I just need to add an extra filter to the filter group.

To achieve this I've created a di.xml as follows:

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
  <type name="Magento\Framework\Api\Search\ReportingInterface">
    <plugin name="customer_grid_collection" type="Vendor\Module\Plugin\Reporting"/>
  </type>
</config>

The plugin class has:

public function beforeSearch(ReportingInterface $subject, SearchCriteriaInterface $searchCriteria)
{
    foreach ($searchCriteria->getFilterGroups() as $filterGroup) {
        foreach ($filterGroup->getFilters() as $filter) {
            if ($myConditionsAreMet) {
                $filterGroup->setFilters(array_merge(
                    $filterGroup->getFilters(),
                    [new Filter([
                        'field' => $filter->getField(),
                        'value' => true,
                        'condition_type' => 'null',
                    ])]
                ));
                return [$searchCriteria];
            }
        }
    }
    return [$searchCriteria];
}

To debug this I have another function in the plugin class:

public function afterSearch(ReportingInterface $subject, SearchResultInterface $collection)
{
    $this->log($collection->getSelect()->__toString());
    return $collection;
}

Actual result (*)

The afterSearch function outputs the following:

SELECT `main_table`.* FROM `customer_grid_flat` AS `main_table` 
WHERE (`my_field` LIKE \'%value%\') AND (`my_field` IS NULL)

The filters are AND'd together. This is the same as #4287 which says that the issue has been fixed. Rather than re-open it I thought I would post my issue here.

Looking at \Magento\Framework\View\Element\UiComponent\DataProvider\FilterPool the applyFilters function loops through each group, then each filter, and applies each filter one at a time, completely ignoring the fact that groups should be processed together. So even if I were to create my own filterApplier here, it won't fix my problem since my applier will receive the filters one at a time.

Expected result (*)

I would expect to see, as per the intended behaviour of FilterGroup, for my filters to be OR'd together like the following:

... WHERE ((`my_field` LIKE \'%value%\') OR (`my_field` IS NULL))

Thank you.

Metadata

Metadata

Assignees

Labels

CDIssue recommended for the contribution dayComponent: Framework/CodeIssue: Clear DescriptionGate 2 Passed. Manual verification of the issue description passedIssue: ConfirmedGate 3 Passed. Manual verification of the issue completed. Issue is confirmedIssue: Format is validGate 1 Passed. Automatic verification of issue format passedIssue: Ready for WorkGate 4. Acknowledged. Issue is added to backlog and ready for developmentPriority: P1Once P0 defects have been fixed, a defect having this priority is the next candidate for fixing.Progress: doneReproduced on 2.3.xThe issue has been reproduced on latest 2.3 releaseSeverity: S1Affects critical data or functionality and forces users to employ a workaround.Triage: Dev.ExperienceIssue related to Developer Experience and needs help with Triage to Confirm or Reject it

Type

No type

Projects

Status

Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions