drupal-hook-patterns

Migrate procedural Drupal hooks to OOP #[Hook] attributes.

1|Updated Mar 22, 2026
One-click install
npx skills add https://github.com/AJV009/drupal-devkit --skill drupal-hook-patterns
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: drupal-hook-patterns
Source: https://github.com/AJV009/drupal-devkit/tree/main/plugins/drupal-core/skills/drupal-hook-patterns
Command: npx skills add https://github.com/AJV009/drupal-devkit --skill drupal-hook-patterns

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Drupal module development often relies on procedural hook implementations in .module files. This skill provides modern OOP hook patterns using #[Hook] attributes and a legacy bridge for Drupal 10 compatibility, enabling maintainable, testable module code.

Core Features & Use Cases

  • OOP Hook pattern with #[Hook] attribute for Drupal 11+.
  • Legacy Bridge for Drupal 10 compatibility.
  • Examples of Form Alter, Entity Hooks, Theme Hooks, and Event Subscribers.
  • Use cases: migrate existing module code to OOP patterns, implement new hooks with clear separation of concerns.

Quick Start

Explain how to convert a legacy hook into an OOP hook using attributes in a module.

Frequently Asked Questions about drupal-hook-patterns

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I convert procedural Drupal hooks to OOP attributes?▼

Converting procedural Drupal hooks to OOP attributes involves replacing .module file implementations with #[Hook] attribute classes. This modern pattern enables maintainable, testable module code through clear separation of concerns in Drupal 11+.

Can I use OOP hook patterns with Drupal 10?▼

Yes, you can use OOP hook patterns with Drupal 10 by implementing a LegacyBridge. This bridge maintains compatibility while allowing you to adopt the #[Hook] attribute pattern before upgrading to Drupal 11.

What Drupal hooks can be implemented using the #[Hook] attribute?▼

The #[Hook] attribute supports form alters, entity hooks, theme hooks, and event subscribers. These OOP patterns replace procedural implementations in .module files while providing clear separation of concerns.

Do I need PHP 8 to use Drupal hook attributes?▼

Yes, PHP 8+ is required to use Drupal hook attributes because the #[Hook] attribute syntax relies on PHP 8 attribute features. You also need Drupal Core 10 or 11 for compatibility.

What's the best way to migrate existing module code to OOP hook patterns?▼

The best way to migrate existing module code to OOP hook patterns is to identify procedural hook implementations in .module files and convert them to #[Hook] attribute classes. Use a LegacyBridge for Drupal 10 compatibility during the transition.

Why migrate from procedural .module hooks to OOP attribute patterns?▼

Migrating from procedural .module hooks to OOP attribute patterns improves maintainability and testability of module code. The #[Hook] attribute approach provides clear separation of concerns compared to traditional procedural implementations.