function hook_views_query_substitutions
Same name and namespace in other branches
- 9 core/modules/views/views.api.php \hook_views_query_substitutions()
- 8.9.x core/modules/views/views.api.php \hook_views_query_substitutions()
- 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
12 functions implement hook_views_query_substitutions()
Note: the procedural functions in this list are found by pattern matching, so the list may include some functions that are not actually implementations of this hook.
- ContentModerationViewsExecutionHooks::viewsQuerySubstitutions in core/
modules/ content_moderation/ src/ Hook/ ContentModerationViewsExecutionHooks.php - Implements hook_views_query_substitutions().
- content_moderation_views_query_substitutions in core/
modules/ content_moderation/ content_moderation.views_execution.inc - Implements hook_views_query_substitutions().
- MediaHooks::viewsQuerySubstitutions in core/
modules/ media/ src/ Hook/ MediaHooks.php - Implements hook_views_query_substitutions().
- media_views_query_substitutions in core/
modules/ media/ media.module - Implements hook_views_query_substitutions().
- NodeViewsExecutionHooks::viewsQuerySubstitutions in core/
modules/ node/ src/ Hook/ NodeViewsExecutionHooks.php - Implements hook_views_query_substitutions().
2 invocations of hook_views_query_substitutions()
- Sql::execute in core/
modules/ views/ src/ Plugin/ views/ query/ Sql.php - Executes the query and fills associated view object with according values.
- Sql::query in core/
modules/ views/ src/ Plugin/ views/ query/ Sql.php - Generates a query and count query from all of the information supplied.
File
-
core/
modules/ views/ views.api.php, line 679
Code
function hook_views_query_substitutions(ViewExecutable $view) {
// Example from views_views_query_substitutions().
return [
'***CURRENT_VERSION***' => \Drupal::VERSION,
'***CURRENT_TIME***' => \Drupal::time()->getRequestTime(),
'***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.