Post

Drupal Preprocess Functions

Preprocess functions are used to modify variables before they are used in the template. This allows developers to customize the output of a page without having to modify the template itself. Preprocess functions are used to add, remove, or alter variables that are used in the template.

Preprocess functions are typically used when you need to modify the variables that are used in the template. For example, if you need to add a custom variable to the template, you can use a preprocess function to do so.

Where to add these functions

themename.theme

Overview

  • preprocess functions are optional - if they can be used they will be if not they will be skipped
  • preprocess functions are Specific to a template (hook) based on naming convention
  • preprocess functions always has 1 param

When to use preprocess functions

  • Use a preprocess function to alter/remove or add variables
  • Make small alterations to the html output
  • Any logic that is more complex then a simple if / else file consider refactoring into a preprocess function to keep template files nice and clean.

Naming Convention

function THEME_preprocess_H00K()

  • HOOK is the name of the template your preprocessing variables for

The easiest way to determine what hook to use is to enable twig debug mode

Example

1
2
3
4
5
function THEMENAME_preprocess_node(&$variables) {
   $variables['simple_string'] = array(
   '#markup' => 'a simple_string'
   );
}
1
2
3
<div class = "string">
   {{simple_string}}
</div>

Example - Add suffix to post author lable if logged in

1
2
3
4
5
function THEMENAME_preprocess_node(&$variables) {
   if($variables['logged_in'] == TRUE && $variables['node']->getOwnerId() == $variables['user']->id()) {
      $variables['label']['#suffix'] = '-you are the author';
   }
}

Example - Add new variable to template

Use twig dump function to see the new variable added to the template

1
2
3
4
5
6
7
8
function THEMENAME_preprocess_node(&$variables) {
   $variables['current_user_is_owner'] = FALSE;

   if($variables['logged_in'] == TRUE && $variables['node']->getOwnerId() == $variables['user']->id()) {
      $variables['label']['#suffix'] = '-you are the author';
      $variables['current_user_is_owner'] = TRUE;
      }
}

Function Naming Conventions

function THEME_preprocess_HOOK()

1
2
**example**
function bartik_preprocess_page()

More specific targeting example

1
2
3
THEMENAME_preprocess_node__42(): This is the most specific version and will only be called for a node with an ID of 42.
THEMENAME_preprocess_node__article(): This would be called for any article node, but not for other node types.
THEMENAME_preprocess_node(): This will be called for all nodes.
  • The hook refers to the base name of the template file
  • Use Twig Debug to find the hook name for the template file

You can ommit the hook part and add hook as param instead, for adding a variable to every template file - warning this will be called alot

Inject var into every template

function THEMENAME_preprocess(&$variables, $hook)

More info

more

This post is licensed under CC BY 4.0 by the author.