Because extension attributes are often loaded after the main record data, standard grid filtering may not work for them. To implement custom filtering for columns containing extension attributes, you can intercept the data loading process using the hyva_grid_source_prefetch_ event.
When the RepositoryGridSourceType is about to load grid data, it dispatches an event named hyva_grid_source_prefetch_ followed by a sanitized version of the grid name (where non-alphanumeric characters like - are replaced with underscores).
How to use this event:
- Identify the event name: If your grid name is
customer-grid, the event name is hyva_grid_source_prefetch_customer_grid. - Create an Observer: Listen for this event in your
events.xml. - Modify Search Criteria: The event provides a
search_criteria_container (which wraps the SearchCriteriaInterface), the grid_name, and the record_type. - Apply Logic: You can use the observer to:
- Modify or replace the
SearchCriteria to include filters for extension attributes. - Map attribute codes to internal column names.
- Store filter values to apply them manually when extension attributes are loaded later in the lifecycle.
// Example of the event dispatch logic in RepositoryGridSourceType
private function dispatchEvent(
string $gridName,
string $recordType,
SearchCriteriaInterface $searchCriteria
): SearchCriteriaInterface {
$eventName = 'hyva_grid_source_prefetch_' . $this->getGridNameEventSuffix($gridName);
$container = new SearchCriteriaEventContainer($searchCriteria);
$this->eventManager->dispatch($eventName, [
'search_criteria_container' => $container,
'grid_name' => $gridName,
'record_type' => $recordType
]);
return $container->getSearchCriteria();
}