hook_views_query_substitutions

hook_views_query_substitutions(ViewExecutable $view)

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

Hooks
Define functions that alter the behavior of Drupal core.

File

core/modules/views/views.api.php, line 660
Describes hooks and plugins provided by the Views module.

Code

function hook_views_query_substitutions(ViewExecutable $view) {
  // Example from views_views_query_substitutions().
  return array(
    '***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(),
  );
}
doc_Drupal
2016-10-29 09:18:40
Comments
Leave a Comment

Please login to continue.