Starting with Drupal 11.5, the .module file extension is deprecated. It will be no longer autoladed in Drupal 13. This means all functions inside this file have to move to classes and services, otherwise they stop running.
Old Implementation
<?php
/**
* @file
* Legacy procedural hooks – deprecated since Drupal 11.5.
*/
use Drupal\Core\Form\FormStateInterface;
use Drupal\node\NodeInterface;
/**
* Implements hook_form_alter().
*/
function my_module_form_alter(array &$form, FormStateInterface $form_state, $form_id) {
if ($form_id === 'user_login_form') {
$form['#attributes']['class'][] = 'my-module-login';
}
}
/**
* Implements hook_ENTITY_TYPE_presave() for node entities.
*/
function my_module_node_presave(NodeInterface $node) {
$node->setTitle(trim($node->getTitle()));
}
New Implementation
<?php
declare(strict_types=1);
namespace Drupal\my_module\Hook;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Hook\Attribute\Hook;
use Drupal\Core\Hook\Order\Order;
use Drupal\node\NodeInterface;
/**
* Hook implementations for my_module.
*/
class MyModuleHooks {
/**
* Implements hook_form_alter().
*/
#[Hook('form_alter', order: Order::Last)]
public function formAlter(array &$form, FormStateInterface $form_state, string $form_id): void {
if ($form_id === 'user_login_form') {
$form['#attributes']['class'][] = 'my-module-login';
}
}
/**
* Implements hook_ENTITY_TYPE_presave() for node entities.
*/
#[Hook('node_presave')]
public function nodePresave(NodeInterface $node): void {
$node->setTitle(trim($node->getTitle()));
}
}
parameters:
# All hooks are OOP now – tell core not to scan .module/.inc files.
my_module.skip_procedural_hook_scan: true
Issue:
Starting with Drupal 11.5, the .module file extension is deprecated. It will be no longer autoladed in Drupal 13. This means all functions inside this file have to move to classes and services, otherwise they stop running.
As long as Core supports procedual hooks, it scans every .module or .inc files for hooks, which costs performance even when a module has nothing procedual left.
Prerequisites:
Solution:
- Hook class: Any class in the
Drupal\my_module\Hook namespace or its subdirectories is registered automatically as an autowired service, so no services.yml entry is needed. The #[Hook('...')] attribute defines which hook a method implements. One method can carry several #[Hook] attributes and an order parameter. To change hooks of other modules you can use #[RemoveHook] or #[ReorderHook]. - Skip the scan: Once no more regular procedual hooks are left in our
services.yml, we set my_module.skip_procedural_hook_scan: true so that Core is skipping parsing our modules .module or .inc files. Procedual hooks that are not #[LegacyHook] would not be found anymore. - Move remaining helper functions to services or methods on the hook class. If other projects call your public functions, keep them as deprecated wrappers that call the new service.
To help you upgrading your code for Drupal 13, there is a ddev script listed in the additional links section.
Additional Links: