Allow better control over query results to get proper Pager info

Created on 22 February 2021, almost 4 years ago
Updated 26 January 2025, 1 day ago

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:

  1. 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.
  2. 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.
  3. Extending \Drupal\views\Plugin\views\query\Sql class. 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

  1. Discuss and decide
  2. 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.

Feature request
Status

Needs work

Version

11.0 🔥

Component

views.module

Created by

🇵🇹Portugal dxvargas

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇳🇱Netherlands johnv

    IMO 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.

  • 🇳🇱Netherlands johnv
Production build 0.71.5 2024