8.3.x theme.api.php hook_preprocess_HOOK(&$variables)
8.0.x theme.api.php hook_preprocess_HOOK(&$variables)
8.1.x theme.api.php hook_preprocess_HOOK(&$variables)
8.2.x theme.api.php hook_preprocess_HOOK(&$variables)
8.4.x theme.api.php hook_preprocess_HOOK(&$variables)
6.x core.php hook_preprocess_HOOK(&$variables)
7.x theme.api.php hook_preprocess_HOOK(&$variables)

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

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

Related topics

190 functions implement hook_preprocess_HOOK()

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

aggregator_preprocess_block in core/modules/aggregator/aggregator.module
Implements hook_preprocess_HOOK() for block templates.
bartik_preprocess_block in core/themes/bartik/bartik.theme
Implements hook_preprocess_HOOK() for block.html.twig.
bartik_preprocess_html in core/themes/bartik/bartik.theme
Implements hook_preprocess_HOOK() for HTML document templates.
bartik_preprocess_maintenance_page in core/themes/bartik/bartik.theme
Implements hook_preprocess_HOOK() for maintenance-page.html.twig.
bartik_preprocess_menu in core/themes/bartik/bartik.theme
Implements hook_preprocess_HOOK() for menu.html.twig.

... See full list

File

core/lib/Drupal/Core/Render/theme.api.php, line 603
Hooks and documentation related to the theme and render system.

Code

function hook_preprocess_HOOK(&$variables) {
  // This example is from rdf_preprocess_image(). It adds an RDF attribute
  // to the image hook's variables.
  $variables['attributes']['typeof'] = array('foaf:Image');
}

Comments

ouelmart’s picture

is there a list of HOOKs, the second one.
i am aware of

- hook_preprocess_node
- hook_preprocess_views_view

what are the others

dimaboychev’s picture

All hook suggestions (not sure if using the right term?) should work.
For example, look at twig template names. To name a few:
- page.html.twig becomes hook_preprocess_page
- page--front.html.twig becomes hook_preprocess_page__front (double hyphen "--" becomes double underscore "__")
- block.html.twig becomes hook_preprocess_block
- block--module.html.twig becomes hook_preprocess_block__module
- field.html.twig becomes hook_preprocess_field

n_potter’s picture

Rather than try and find a list of hooks, turn on theme debugging:

https://www.drupal.org/docs/8/theming/twig/debugging-twig-templates

You can then see the name of the theme hook for every element on the page that has it implemented, in an HTML comment. It will read like this:

THEME HOOK: 'block'

Then, you can write the preprocess function as hook_preprocess_HOOK, remembering to substitute the module/theme name and hook. For example, for the above:

mymodule_preprocess_block(&$variables) {

}

jerrik’s picture

s_xenikakis’s picture

You can preprocess for example main menu with theme_preprocess_menu__main(&$variables){} and then override default menu.html.twig template with menu--main.html.twig . The values should be passed directly to hook.

npcoder’s picture

I have created a content type 'About'. The node resides in the content type are 'About us', 'Director Message', 'Contact us' and 'Calendar'. Contact us and Calendar have the different layout whereas other has the same layout.

I have setup debug. So it suggested me 'page--about.html.twig' , which is not my desire theme page.
I'm expecting as 'page--contact-us.html.twig' and 'page--calendar.html.twig'.

Your suggestion will be more helpful for me.

Thanks.