Problem/Motivation
Implementing hook_views_query_alter allows unsetting a WHERE condition like this:
function mymodule_views_query_alter(ViewExecutable $view, QueryPluginBase $query) {
unset($query->where[0]);
}
The equivalent code implemented via the VIEWS_QUERY_ALTER event subscriber does not work:
public function viewsQueryAlter(ViewsQueryAlterEvent $event): void {
$query = $event->getQuery();
unset($query->where[0]);
}
The code runs and no error is thrown, but the view query is not altered (the results are not affected). This it not a result of a bad implementation on my part. I have step debugged this and I can see that it runs when expected. I can affect the view results by using methods of the QueryPluginBase class (for example $query->addWhere()), but I cannot directly unset a WHERE condition the same way that I can when using the equivalent Drupal core hook.
Regardless of whether you think unsetting a WHERE condition this way is correct (there is no method to do it, so who can say?), I think the event should behave exactly the same way as the core hook, or else there are bound to be issues when converting hooks to events. Until this is fixed, I'm unfortunately stuck using the hook.
I suspect that this has something to do with not passing $query by reference, but I've looked into replicating the way $form is passed from the form alter event, and haven't had any luck.
Steps to reproduce
Proposed resolution
Remaining tasks
User interface changes
API changes
Data model changes