Drupal is a registered trademark of Dries Buytaert
Protected Pages 3.0.0 Major update available for module protected_pages (3.0.0). Commerce Core 3.3.8 Minor update available for module commerce (3.3.8). Search API HTML Element Filter 1.0.7 Minor update available for module search_api_html_element_filter (1.0.7). Layout Builder Reorder 2.0.1 Minor update available for module layout_builder_reorder (2.0.1). Ban 1.1.0 Minor update available for module ban (1.1.0). Field Formatter Range 2.0.0 Major update available for module field_formatter_range (2.0.0). Field Formatter Range 8.x-1.8 Minor update available for module field_formatter_range (8.x-1.8). Varbase Media Header 9.2.1 Minor update available for module varbase_media_header (9.2.1). Search API Solr 4.3.11 Module search_api_solr updated after 14 months of inactivity (4.3.11). Provus Mega Menu Module provus_mega_menu crossed 1,000 active installs.

Preprocessor Files

7 sites Security covered
View on drupal.org

This module allows you to create separate PHP files for preprocessing template variables for specific Twig templates. This helps keep your theme's main files cleaner by isolating preprocessing logic for each template.

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
Releases (12 mo)
0 ▼ from 1
Maintenance
Dormant

Releases

Version Type Release date
1.2.3 Stable Feb 9, 2025