function hook_preprocess_HOOK

Same name and namespace in other branches
  1. 10 core/lib/Drupal/Core/Render/theme.api.php \hook_preprocess_HOOK()
  2. 9 core/lib/Drupal/Core/Render/theme.api.php \hook_preprocess_HOOK()
  3. 8.9.x core/lib/Drupal/Core/Render/theme.api.php \hook_preprocess_HOOK()
  4. 7.x modules/system/theme.api.php \hook_preprocess_HOOK()
  5. main core/lib/Drupal/Core/Render/theme.api.php \hook_preprocess_HOOK()

Preprocess theme variables for a specific theme hook.

This hook allows modules to preprocess theme variables for a specific theme hook. It should only be used if a module needs to override or add to the theme preprocessing for a theme hook it didn't define.

For more detailed information, see the Theme system overview topic.

Parameters

array $variables: The variables array (modify in place).

Related topics

142 functions implement hook_preprocess_HOOK()

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.

module_test_procedural_preprocess_preprocess in core/modules/system/tests/modules/module_test_procedural_preprocess/module_test_procedural_preprocess.module
module_test_procedural_preprocess_preprocess_test in core/modules/system/tests/modules/module_test_procedural_preprocess/module_test_procedural_preprocess.module
module_test_procedural_preprocess_theme in core/modules/system/tests/modules/module_test_procedural_preprocess/module_test_procedural_preprocess.module
Implements hook_theme().
stable9_preprocess_item_list__search_results in core/themes/stable9/stable9.theme
Implements hook_preprocess_item_list__search_results().
stable9_preprocess_views_view in core/themes/stable9/stable9.theme
Implements hook_preprocess_views_view().

... See full list

File

core/lib/Drupal/Core/Render/theme.api.php, line 610

Code

function hook_preprocess_HOOK(&$variables) : void {
  // This example is from \Drupal\node\Hook\NodeThemeHooks::preprocessHtml().
  // It adds the node type to the body classes, when on an individual node page
  // or node preview page.
  if (($node = \Drupal::routeMatch()->getParameter('node')) || $node = \Drupal::routeMatch()->getParameter('node_preview')) {
    if ($node instanceof NodeInterface) {
      $variables['node_type'] = $node->getType();
    }
  }
}

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