Problem/Motivation
There may be a need to further filter the results right after executing the query.
In my case, I have a custom logic and I need to apply it to a contrib view, to filter its results.
I've considered the existing options:
- Adding filters and relations to the original query was the first one. In the end I would have a very complicated query that wouldn't perform well. Also my logic is already encoded in PHP, in a handler class and it's not good to implement the (hardly) same logic in SQL.
- Using a hook. Right now, the first hook available after executing the query is "views_post_execute". But by then the pager and total rows info is updated, the postExecute method is run for all active handlers and the cache is set with the results. If we want to strip some results, this should be performed again.
- Extending
\Drupal\views\Plugin\views\query\Sqlclass. This was my choice. The only problem I found is that the execute method is long and it performs many operations that I don't need to change.
My proposal here is to discuss what is the best way to achieve the goal to filter results and if we can improve views to make it easier.
Proposed resolution
One improvement is needed is to breakdown the \Drupal\views\Plugin\views\query\Sql::execute method that is too long.
I also suggest enabling a new hook to allow filtering easily.
I'll submit patches with these suggestions after.
Remaining tasks
- Discuss and decide
- Improve
API changes
May be a new hook, hook_view_query_result. To be decided.
Release notes snippet
For now we only know that no backwards compatibility issues are foreseen.
| Comment | File | Size | Author |
|---|---|---|---|
| #4 | 3199661-4.patch | 2.45 KB | dxvargas |
| #2 | 3199661-2.patch | 2.18 KB | dxvargas |
Issue fork drupal-3199661
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #2
dxvargas commentedIn this patch I'm breaking the
\Drupal\views\Plugin\views\query\Sql::executemethod to execute the query in a new method:\Drupal\views\Plugin\views\query\Sql::doExecute.This alone will allow to easily extend this method in a custom Sql class that may extend the original in a cleaner way.
There, a developer can rework the results.
But I also added a new hook "views_query_result" that allows the same goal using this hook. This is a nice-to-have but not really necessary. Of course we would then need to add this information in views.api.php.
For this propose I suggest to run the method loadEntities before the block
$this->pager->postExecute.Like this the hook will already have view with the entities attached and this allows the pager to run with the changed results.
Comment #3
dxvargas commentedComment #4
dxvargas commentedMinor fixes and hope the patch succeeds to be applied now.
Comment #10
johnvIMO you should use the 'views_post_execute' hook, AND the Sql class should be changed (not overridden - changed), since there are multiple issues with Full Pager and filtering after the SQL query.
The pager info should be generated AFTER the 'views_post_execute'.
I'm pruning all core issues with 'pager'/'pagination' and 'filter'. From all this crowd wisdom someone should have published the best way to correct the problem.
Comment #11
johnv