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.

CommentFileSizeAuthor
#4 3199661-4.patch2.45 KBdxvargas
#2 3199661-2.patch2.18 KBdxvargas

Issue fork drupal-3199661

Command icon 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:

  • 9.2.x Comparecompare
  • 3199661-allow-better-control Comparecompare

Comments

dxvargas created an issue. See original summary.

dxvargas’s picture

StatusFileSize
new2.18 KB

In this patch I'm breaking the \Drupal\views\Plugin\views\query\Sql::execute method 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.

dxvargas’s picture

Issue summary: View changes
dxvargas’s picture

StatusFileSize
new2.45 KB

Minor fixes and hope the patch succeeds to be applied now.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

johnv’s picture

Title: Allow better control over query results » Allow better control over query results to get proper Pager info
Status: Active » Needs work

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.

johnv’s picture

Category: Feature request » Bug report

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.