Skip to content

Instantly share code, notes, and snippets.

@rpayanm
Last active July 31, 2019 14:16
Show Gist options
  • Save rpayanm/20c26eed3df9023a4d33e8332ef5e554 to your computer and use it in GitHub Desktop.
Save rpayanm/20c26eed3df9023a4d33e8332ef5e554 to your computer and use it in GitHub Desktop.

There are two ways to discover the hook name of a specific component. With the hook name, you can locate its base template file.

First, if you already know the filename of the template, you can infer the hook name from that.

The typical pattern for naming template files is {hook}--{optional context}.html.twig

Hook name dicovery

You can ignore the .html.twig suffix. Which results in {hook}--{optional context}

If the file name doesn't contain a --{optional context} part, then after removing .html.twig you're left with the hook name.

Examples:

  • node.html.twig: hook name is node
  • node--blog_post.html.twig: hook name is node
  • field--node--title--page.html.twig: hook name is field

Preprocess functions

Preprocess functions follow a specific naming convention:

THEMENAME_preprocess_HOOK() or HOOK_preprocess_HOOK()

HOOK is roughly the name of the template file for which you want to preprocess data.

The complete list of preprocess functions called for a template file is below, listed in the order they are called (if they exist):

  • template_preprocess(&$variables, $hook): Creates a default set of variables for all theme hooks with template implementations. Provided by Drupal Core.
  • template_preprocess_HOOK(&$variables): Should be implemented by the module that registers the theme hook, to set up default variables.
  • MODULE_preprocess(&$variables, $hook): hook_preprocess() is invoked on all implementing modules.
  • MODULE_preprocess_HOOK(&$variables): hook_preprocess_HOOK() is invoked on all implementing modules, so that modules that didn't define the theme hook can alter the variables.
  • ENGINE_engine_preprocess(&$variables, $hook): Allows the theme engine to set necessary variables for all theme hooks with template implementations.
  • ENGINE_engine_preprocess_HOOK(&$variables): Allows the theme engine to set necessary variables for the particular theme hook.
  • THEME_preprocess(&$variables, $hook): Allows the theme to set necessary variables for all theme hooks with template implementations.
  • THEME_preprocess_HOOK(&$variables): Allows the theme to set necessary variables specific to the particular theme hook.

Source:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment