Skip to content

Instantly share code, notes, and snippets.

@oksana-c
Last active November 8, 2018 22:07
Show Gist options
  • Save oksana-c/27f9d14cf99a9ea8ddf03d103078ccb0 to your computer and use it in GitHub Desktop.
Save oksana-c/27f9d14cf99a9ea8ddf03d103078ccb0 to your computer and use it in GitHub Desktop.
D8: Defining preprocess & process hooks

contents of the theme/preprocess/README.md

Defining preprocess hooks

Rather than placing your preprocess hooks directly in the .theme file you can manage them in automatically discovered and lazy-loaded include files. It is even possible to organize them in sub-folders. This feature greatly improves the maintainability of large themes that would otherwise contain hundreds of lines of unrelated code in your template.php file.

The include files have to follow a certain naming pattern (HOOK.preprocess.inc) for them to be automatically discovered:

  • THEMENAME_preprocess_html() = html.preprocess.inc
  • THEMENAME_preprocess_page() = page.preprocess.inc
  • THEMENAME_preprocess_node() = node.preprocess.inc
  • THEMENAME_preprocess_comment() = comment.preprocess.inc
  • THEMENAME_preprocess_region() = region.preprocess.inc

As with template files, you should replace underscores from the hook names with hyphens:

  • THEMENAME_preprocess_comment_wrapper() = comment-wrapper.preprocess.inc
  • THEMENAME_preprocess_html_tag() = html-tag.preprocess.inc

Inside of each of these files you define the preprocess hook just as you would otherwise do in your template.php file:

function THEMENAME_preprocess_HOOK(&$variables) {
  // Your code here.
}

contents of the theme/process/README.md

Defining process hooks

Rather than placing your process hooks directly in the .theme file you can manage them in automatically discovered and lazy-loaded include files. It is even possible to organize them in sub-folders. This feature greatly improves the maintainability of large themes that would otherwise contain hundreds of lines of unrelated code in your template.php file.

The include files have to follow a certain naming pattern (HOOK.process.inc) for them to be automatically discovered:

  • THEMENAME_process_html() = html.process.inc
  • THEMENAME_process_page() = page.process.inc
  • THEMENAME_process_node() = node.process.inc
  • THEMENAME_process_comment() = comment.process.inc
  • THEMENAME_process_region() = region.process.inc

As with template files, you should replace underscores from the hook names with hyphens:

  • THEMENAME_process_comment_wrapper() = comment-wrapper.process.inc
  • THEMENAME_process_html_tag() = html-tag.process.inc

Inside of each of these files you define the process hook just as you would otherwise do in your template.php file:

function THEMENAME_process_HOOK(&$variables) {
  // Your code here.
}

Theme directory structure

Theme directory structure when preprocess & process hooks are moved out of the .theme file into dedicated directories becomes the following:

|-fluffiness.breakpoints.yml
  |-fluffiness.info.yml
  |-fluffiness.libraries.yml
  |-fluffiness.theme
  |-config
  |  |-install
  |  |  |-fluffiness.settings.yml
  |  |-schema
  |  |  |-fluffiness.schema.yml
  |-css
  |  |-style.css
  |-js
  |  |-fluffiness.js
  |-images
  |  |-buttons.png
  |-logo.svg
  |-preprocess
  |  |-README.md
  |-process
  |  |-README.md
  |-screenshot.png
  |-templates
  |  |-maintenance-page.html.twig
  |  |-node.html.twig
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment