Preprocess Functions in Drupal Themes

Preprocess functions in a Drupal theme prepare variables for use in template files, and theme developers need to know how to use them to manipulate and customize data before it is rendered, ensuring greater flexibility and control over the site's presentation.

This course covers the fundamental concepts of preprocess functions, including their purpose, how to define them, and practical applications for adding and changing variables. By the end of the course, learners will be equipped with the skills to implement preprocess functions in their themes, enabling them to customize and optimize the rendering of dynamic content in Drupal.

Key topics

  • What are preprocess functions and how to add one
  • Putting a theme’s PHP logic into .theme files
  • Modifying existing template variables
  • Adding new template variables
  • Important naming conventions
  • Practical examples
Tutorials in this course
Categories
Drupal 8, 9, and 10
More information

Every theme can contain an optional THEMENAME.theme file. This file contains additional business logic written in PHP and is primarily used for manipulation of the variables available for a template file, and suggesting alternative candidate template file names. Themes can also use this file to implement some, but not all, of the hooks invoked by Drupal modules.

In this tutorial we'll learn:

  • The use case for THEMENAME.theme files, and where to find them
  • The different types of functions and hooks you can implement in a THEMENAME.theme file

By the end of this tutorial you should be able to know how to start adding PHP logic to your custom theme.

Categories
Drupal 8, 9, and 10
More information

Preprocess functions allow Drupal themes to manipulate the variables that are used in Twig template files by using PHP functions to preprocess data before it is exposed to each template. All of the dynamic content available to theme developers within a Twig template file is exposed through a preprocess function. Understanding how preprocess functions work, and the role they play, is important for both module developers and theme developers.

In this tutorial we'll learn:

  • What preprocess functions are and how they work
  • The use case for preprocess functions
  • The order of execution for preprocess functions

By the end of this tutorial you should be able to explain what preprocess functions are and the role they play in a Drupal theme.

More information

Preprocess functions are specially-named PHP functions that allow themes and modules to modify the variables passed to a Twig template file. They are commonly used by themes to alter existing variables before they are passed to the relevant template files. For example; Changing the makeup of render array so that it renders an <ol> list instead of a <ul> list. Or appending data to the label of a node depending on custom logic.

In this tutorial we'll:

  • Define a new preprocess function in our theme's .theme file
  • Use the preprocess functions to modify the content of an existing variable before it's used in Twig

By the end of this tutorial you should be able to define new preprocess functions in a theme (or module) that manipulate the variables for a specific Twig template file.

Categories
Drupal 8, 9, and 10
More information

Preprocess functions are specially-named functions that can be used to add new variables to a Twig template file. They are commonly used by themes to add new variables based on custom PHP logic and simplify accessing the data contained in complex entity structures. For example: adding a variable to all node.html.twig template files that contains the combined content a couple of specific fields under a meaningful name like {{ call_to_action }}. Modules use preprocess functions to expose the dynamic data they manage to Twig template files, or to alter data provided by another module based on custom logic.

In this tutorial we'll learn how to:

  • Use PHP to perform some complex logic in our theme.
  • Store the resulting calculation in a variable.
  • Make that variable available to a Twig template file.

Example use cases for adding variables with preprocess functions include:

  • Anytime calculating the value to output in a template requires logic more complex than an if/else statement.
  • Anytime the desired value requires additional string manipulation beyond what can be easily accomplished using an existing Twig filter or function.

By the end of this tutorial you should be able to expose new variables to a Twig template file by defining a preprocess function in either a module or a theme.

This course appears in the following guides:
Theming
Learn to control the look, feel, and markup of a Drupal site in a theme.

Frontend Theming