$npx -y skills add edutrul/drupal-ai --skill drupal-hooksDrupal 11 OOP and procedural hooks — hook_form_alter, hook_node_presave, hook_theme, #[Hook] attribute, and when to use hooks vs event subscribers.
| 1 | # Drupal Hooks (Drupal 11) |
| 2 | |
| 3 | ## OOP Hooks (Preferred) |
| 4 | |
| 5 | | | | |
| 6 | |---|---| |
| 7 | | **Location** | `src/Hook/MyModuleHooks.php` | |
| 8 | | **Namespace** | `Drupal\my_module\Hook` | |
| 9 | | **Auto-registered** | Drupal 11.1+ (no services.yml needed) | |
| 10 | | **DI support** | Constructor injection | |
| 11 | | **Testable** | Yes — instantiate directly, inject mocks | |
| 12 | |
| 13 | ## Example (Best Practice) |
| 14 | |
| 15 | ```php |
| 16 | <?php |
| 17 | |
| 18 | namespace Drupal\my_module\Hook; |
| 19 | |
| 20 | use Drupal\Core\Form\FormStateInterface; |
| 21 | use Drupal\Core\Hook\Attribute\Hook; |
| 22 | use Drupal\Core\Session\AccountProxyInterface; |
| 23 | use Drupal\Core\StringTranslation\StringTranslationTrait; |
| 24 | use Drupal\node\NodeInterface; |
| 25 | |
| 26 | final class MyModuleHooks { |
| 27 | |
| 28 | use StringTranslationTrait; // Provides $this->t(). |
| 29 | |
| 30 | public function __construct( |
| 31 | private readonly AccountProxyInterface $currentUser, |
| 32 | ) {} |
| 33 | |
| 34 | /** |
| 35 | * Implements hook_form_node_article_form_alter(). |
| 36 | * |
| 37 | * Prefer targeted form hooks over generic form_alter — they only fire for |
| 38 | * the specific form ID and avoid unnecessary processing. |
| 39 | */ |
| 40 | #[Hook('form_node_article_form_alter')] |
| 41 | public function formNodeArticleFormAlter(array &$form, FormStateInterface $form_state): void { |
| 42 | $form['title']['#title'] = $this->t('Article title'); |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Implements hook_form_alter(). |
| 47 | * |
| 48 | * Use only when acting on multiple forms or the form ID is unknown at |
| 49 | * development time. Check $form_id explicitly. |
| 50 | */ |
| 51 | #[Hook('form_alter')] |
| 52 | public function formAlter(array &$form, FormStateInterface $form_state, string $form_id): void { |
| 53 | if ($form_id === 'node_page_form') { |
| 54 | $form['title']['#description'] = $this->t('Enter a descriptive page title.'); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | #[Hook('node_presave')] |
| 59 | public function nodePresave(NodeInterface $node): void { |
| 60 | if ($node->getType() === 'article') { |
| 61 | // Example: stamp the current user's ID on save. |
| 62 | $node->set('uid', $this->currentUser->id()); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | #[Hook('theme')] |
| 67 | public function theme(): array { |
| 68 | return [ |
| 69 | 'my_template' => [ |
| 70 | 'variables' => ['content' => NULL], |
| 71 | ], |
| 72 | ]; |
| 73 | } |
| 74 | |
| 75 | } |
| 76 | ``` |
| 77 | |
| 78 | > **Note:** If a hook is not executed, verify namespace and location. OOP hooks outside `src/Hook/` require manual service registration. |
| 79 | |
| 80 | ## services.yml |
| 81 | |
| 82 | | Scenario | Required? | |
| 83 | |---|---| |
| 84 | | Class in `src/Hook/`, namespace `Drupal\my_module\Hook` | No — auto-registered (Drupal 11.1+) | |
| 85 | | Class outside `src/Hook/` (e.g. a custom service) | Yes — register manually | |
| 86 | |
| 87 | When registration is needed — autowire resolves constructor dependencies by type hint: |
| 88 | |
| 89 | ```php |
| 90 | namespace Drupal\my_module\Service; |
| 91 | |
| 92 | use Drupal\Core\Hook\Attribute\Hook; |
| 93 | use Drupal\Core\Session\AccountProxyInterface; |
| 94 | |
| 95 | final class MyCustomService { |
| 96 | |
| 97 | public function __construct( |
| 98 | private readonly AccountProxyInterface $currentUser, |
| 99 | ) {} |
| 100 | |
| 101 | #[Hook('form_alter')] |
| 102 | public function formAlter(...): void { |
| 103 | // ... |
| 104 | } |
| 105 | |
| 106 | } |
| 107 | ``` |
| 108 | |
| 109 | ```yaml |
| 110 | services: |
| 111 | Drupal\my_module\Service\MyCustomService: |
| 112 | autowire: true |
| 113 | ``` |
| 114 | |
| 115 | ## Procedural Hooks (.module) |
| 116 | |
| 117 | Auto-discovered via `my_module_hook_name()` naming. Still valid; prefer OOP hooks for new code. |
| 118 | |
| 119 | ## Hooks vs Event Subscribers |
| 120 | |
| 121 | | Use Hooks When | Use Event Subscribers When | |
| 122 | |---|---| |
| 123 | | `form_alter`, entity hooks, theme hooks | reacting to dispatched Symfony events | |
| 124 | | extending Drupal core/contrib behavior | PSR-14 / decoupled systems | |
| 125 | | working with existing Drupal APIs | building loosely coupled logic | |
| 126 | |
| 127 | > OOP hooks in `src/Hook/` are fully testable — instantiate the class directly and inject mocked dependencies. |
| 128 | |
| 129 | ## RULES (IMPORTANT) |
| 130 | |
| 131 | - ALWAYS prefer OOP hooks in `src/Hook/` for new implementations |
| 132 | - NEVER add new hooks in `.module` unless explicitly requested |
| 133 | - If procedural hook exists, suggest refactoring to OOP |