8.5.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.3.x theme.api.php hook_preprocess_HOOK(&$variables)
8.4.x theme.api.php hook_preprocess_HOOK(&$variables)
8.6.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.


$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


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


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(


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:


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.


qqboy’s picture

how to change a menu's href by xx_preprocess_menu

victorkurauchi’s picture

Sorry for the newbie question, but i've just started with drupal 2 days ago. I'm creating a custom theme and want to use this hook_preprocess functions.

Where do i need to put my .php file with the code `hook_preprocess_page__front()` in, and which name convention should i use for the file ?


markconroy’s picture

If you want to put it in your theme, you put it in your theme's .theme file.

So, if you theme is called my_theme, it goes in my_theme.theme and the function name will look like this:
my_theme_preprocess_FOO(&$variables) {}