Introduced in Drupal 11.3.0
Drupal core continues its modernization journey, and one of the latest updates affects how Views plugins define their template preprocess functions.
If your Views plugin currently uses the theme_file key in its plugin definition — it’s time to update your code.
🔍 What Has Changed
In previous Drupal versions, Views plugins could include a key like this in their plugin annotation:
theme_file = "my_module.theme.inc"
This file would contain preprocess functions such as:
function template_preprocess_views_view_field__my_plugin(&$variables) {
// Prepare data for the template.
}
The theme_file mechanism told Drupal where to find those functions.
Starting from Drupal 11.3.0, this approach is deprecated and will be completely removed in Drupal 12.0.
💡 Why It Was Deprecated
The Drupal team has streamlined how preprocess functions are registered.
Today, all preprocess callbacks are expected to be explicitly registered via hook_theme() or defined directly in object-oriented (OOP) preprocess methods within the plugin class itself.
This change brings consistency across the system and reduces the number of “magical” lookups that Drupal used to perform behind the scenes.
🧭 How to Update Your Code
You have two options for modernizing your Views plugins:
Option 1: Minimal Fix (Move to .module File)
You can simply move your existing preprocess functions from the old *.theme.inc file into your main module file:
function my_module_preprocess_views_view_field__my_plugin(&$variables) {
// Prepare your data here.
}
As long as the corresponding theme hook is registered, Drupal will automatically discover this function.
Option 2: Recommended Fix (OOP Preprocess Method)
The preferred modern approach is to define a preprocess() method directly inside your plugin class:
namespace Drupal\my_module\Plugin\views\field;
use Drupal\views\Plugin\views\field\FieldPluginBase;
/
* Provides a custom field plugin.
*
* @ViewsField("my_plugin")
*/
class MyPlugin extends FieldPluginBase {
/
* Preprocesses template variables for this plugin.
*/
public function preprocess(array &$variables) {
// Your preprocessing logic here.
}
}
This object-oriented style makes the plugin self-contained, easier to read, and more consistent with how Drupal handles templates in general.
🧱 Summary
| Old behavior | New behavior |
|---|---|
theme_file = "..." in plugin annotation | Deprecated |
Preprocess functions in .theme.inc | Deprecated |
Preprocess functions in .module | Still valid |
preprocess() method in the plugin class | Recommended |
🚀 Final Thoughts
If your module defines custom Views plugins, check them for theme_file usage now.
Removing it is a simple change, but keeping your codebase aligned with Drupal’s evolving standards ensures smoother upgrades — and fewer surprises when Drupal 12 arrives.
Comments