function hook_views_query_substitutions

Same name in other branches
  1. 9 core/modules/views/views.api.php \hook_views_query_substitutions()
  2. 10 core/modules/views/views.api.php \hook_views_query_substitutions()
  3. 11.x core/modules/views/views.api.php \hook_views_query_substitutions()

Replace special strings in the query before it is executed.

The idea is that certain dynamic values can be placed in a query when it is built, and substituted at run-time, allowing the query to be cached and still work correctly when executed.

Parameters

\Drupal\views\ViewExecutable $view: The View being executed.

Return value

array An associative array where each key is a string to be replaced, and the corresponding value is its replacement. The strings to replace are often surrounded with '***', as illustrated in the example implementation, to avoid collisions with other values in the query.

Related topics

6 functions implement hook_views_query_substitutions()

Note: this list is generated by pattern matching, so it may include some functions that are not actually implementations of this hook.

content_moderation_views_query_substitutions in core/modules/content_moderation/content_moderation.views_execution.inc
Implements hook_views_query_substitutions().
media_views_query_substitutions in core/modules/media/media.module
Implements hook_views_query_substitutions().
node_views_query_substitutions in core/modules/node/node.views_execution.inc
Implements hook_views_query_substitutions().
user_views_query_substitutions in core/modules/user/user.views_execution.inc
Implements hook_views_query_substitutions().
views_test_data_views_query_substitutions in core/modules/views/tests/modules/views_test_data/views_test_data.views_execution.inc
Implements hook_views_query_substitutions().

... See full list

2 invocations of hook_views_query_substitutions()
Sql::execute in core/modules/views/src/Plugin/views/query/Sql.php
Executes the query and fills the associated view object with according values.
Sql::query in core/modules/views/src/Plugin/views/query/Sql.php
Generate a query and a countquery from all of the information supplied to the object.

File

core/modules/views/views.api.php, line 660

Code

function hook_views_query_substitutions(ViewExecutable $view) {
    // Example from views_views_query_substitutions().
    return [
        '***CURRENT_VERSION***' => \Drupal::VERSION,
        '***CURRENT_TIME***' => REQUEST_TIME,
        '***LANGUAGE_language_content***' => \Drupal::languageManager()->getCurrentLanguage(LanguageInterface::TYPE_CONTENT)
            ->getId(),
        PluginBase::VIEWS_QUERY_LANGUAGE_SITE_DEFAULT => \Drupal::languageManager()->getDefaultLanguage()
            ->getId(),
    ];
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.