Add metadata to the views query

Created on 13 May 2024, 4 months ago
Updated 28 July 2024, about 1 month ago

Problem/Motivation

I use hook_form_alter() to alter the views exposed form and to change the widget of some filter. But more than that I need to change related condition in views query as well. To do that properly I need some params, but for now there is now good way to pass params to the query. So I can access the $view (ViewExecutable) object from form, but found no way to add params to it that can be later used in hook_query_alter().

Proposed resolution

As I can see the $views->query is a Drupal\views\Plugin\views\query\Sql object, and it has the query() method. That method constructs the actual query like this: $query = $this->getConnection()->select(...). And then adds some metadata to it like this: $query->addMetaData(). Looks like this is a good way to pass some key => value data to the query.

So it would be great to add the Drupal\views\Plugin\views\query\Sql::addMetaData() method just like the addTag() method that it already has. And then inside the query() method use collected metadata to pass it to the actual sql query object (just like it does with the tags). And then it would be possible to use getMetaData() in hook_query_alter() to apply some advanced logic to the query conditions.

โœจ Feature request
Status

Needs work

Version

11.0 ๐Ÿ”ฅ

Component
Viewsย  โ†’

Last updated about 11 hours ago

Created by

๐Ÿ‡ท๐Ÿ‡บRussia i-grou

Live updates comments and jobs are added and updated live.
  • Needs tests

    The change is currently missing an automated test that fails when run with the original code, and succeeds when the bug has been fixed.

Sign in to follow issues

Merge Requests

Comments & Activities

Production build 0.71.5 2024