Drupal is a registered trademark of Dries Buytaert

ppf

8 sites Security covered
View on drupal.org

The Preprocessor Files module allows you to create dedicated files to preprocess templates instead of your usual hooks.

Similar to Preprocess Functions, Preprocessor Files can be used to preprocess variables for any given template. For a template like node--article.html.twig, create a respective node--article.preprocess.php and alter your variables in a dedicated file.

The primary goal of this module is to prevent having extremely large .theme or .module files, instead isolating preprocessing for objects in dedicated files.

Documentation

Refer to the documentation to get started, or read the quickstart section below to learn how to create a simple preprocessor in your themes.

Quickstart

Let's take a look at a quick comparison between a traditional preprocess function and a preprocess file.

Traditionally, you would preprocess a node template using the following code:

/**
 * Implements hook_preprocess_HOOK().
 */
function MY_THEME_preprocess_node(&$variables) {
  $variables['foo'] = 'bar';
}

To achieve the same functionality as above within your theme with a file, you would do the following:

Below is an example file that would preprocess a node template.

YOUR_THEME/preprocessors/node.preprocessor.php

/**
 * @file
 * Preprocess override for a template. Acts as a preprocess function.
 *
 * Any modifications done in this file are equivalent to doing them within a
 * hook_preprocess_HOOK() implementation.
 *
 * The modifications here execute after traditional hooks if both are
 * implemented.
 *
 * The loading of this file is provided by the Preprocess Files module.
 * @see \Drupal\ppf\PreprocessFiles
 *
 * Available variables:
 * @var array $variables
 *   The variables array. The same you would get from a traditional preprocess
 *   hook function.
 * @var string $hook
 *   The theme hook.
 * @var array $info
 *   The info array.
 */

// Preprocess your variables here just like you would in a hook!
$variables['foo'] = 'bar';

Visiting the module's configuration page at /admin/config/ppf allows you to generate a starter folder in your default theme.

Configurations allow you to customize the file extension if you wish to do so.

Activity

Total releases
1
First release
Feb 2025
Latest release
1 year ago
Release cadence
Stability
100% stable

Releases

Version Type Release date
1.2.3 Stable Feb 9, 2025