agentleFS
edutruledutrul/drupal-ai/.claude/skills/drupal-hooks/SKILL.md

drupal-hooks

Skill71 starsChanged 7 months agoMITView on GitHub

Drupal 11 OOP and procedural hooks — hook_form_alter, hook_node_presave, hook_theme, #[Hook] attribute, and when to use hooks vs event subscribers.

The file

We split this skill into the rules it gives an agent

4 rules. About 0 are common, 2 are distinctive, and 2 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈2

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 2

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    When registration is needed — autowire resolves constructor dependencies by type hint:

    line 92
  2. Very specific:

    ALWAYS prefer OOP hooks in src/Hook/ for new implementations

    line 136
  3. Very specific:

    NEVER add new hooks in .module unless explicitly requested

    line 137
  4. Distinctive:

    If procedural hook exists, suggest refactoring to OOP

    line 138

≈ “Common” is an estimate: at least 1 in 5 of the 350,495 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Drupal Hooks (Drupal 11)

OOP Hooks (Preferred)

Locationsrc/Hook/MyModuleHooks.php
NamespaceDrupal\my_module\Hook
Auto-registeredDrupal 11.1+ (no services.yml needed)
DI supportConstructor injection
TestableYes — instantiate directly, inject mocks

Example (Best Practice)

<?php

namespace Drupal\my_module\Hook;

use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Hook\Attribute\Hook;
use Drupal\Core\Session\AccountProxyInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\node\NodeInterface;

final class MyModuleHooks {

  use StringTranslationTrait; // Provides $this->t().

  public function __construct(
    private readonly AccountProxyInterface $currentUser,
  ) {}

  /**
   * Implements hook_form_node_article_form_alter().
   *
   * Prefer targeted form hooks over generic form_alter — they only fire for
   * the specific form ID and avoid unnecessary processing.
   */
  #[Hook('form_node_article_form_alter')]
  public function formNodeArticleFormAlter(array &$form, FormStateInterface $form_state): void {
    $form['title']['#title'] = $this->t('Article title');
  }

  /**
   * Implements hook_form_alter().
   *
   * Use only when acting on multiple forms or the form ID is unknown at
   * development time. Check $form_id explicitly.
   */
  #[Hook('form_alter')]
  public function formAlter(array &$form, FormStateInterface $form_state, string $form_id): void {
    if ($form_id === 'node_page_form') {
      $form['title']['#description'] = $this->t('Enter a descriptive page title.');
    }
  }

  #[Hook('node_presave')]
  public function nodePresave(NodeInterface $node): void {
    if ($node->getType() === 'article') {
      // Example: stamp the current user's ID on save.
      $node->set('uid', $this->currentUser->id());
    }
  }

  #[Hook('theme')]
  public function theme(): array {
    return [
      'my_template' => [
        'variables' => ['content' => NULL],
      ],
    ];
  }

}

Note: If a hook is not executed, verify namespace and location. OOP hooks outside src/Hook/ require manual service registration.

services.yml

ScenarioRequired?
Class in src/Hook/, namespace Drupal\my_module\HookNo — auto-registered (Drupal 11.1+)
Class outside src/Hook/ (e.g. a custom service)Yes — register manually

When registration is needed — autowire resolves constructor dependencies by type hint:

namespace Drupal\my_module\Service;

use Drupal\Core\Hook\Attribute\Hook;
use Drupal\Core\Session\AccountProxyInterface;

final class MyCustomService {

  public function __construct(
    private readonly AccountProxyInterface $currentUser,
  ) {}

  #[Hook('form_alter')]
  public function formAlter(...): void {
    // ...
  }

}
services:
  Drupal\my_module\Service\MyCustomService:
    autowire: true

Procedural Hooks (.module)

Auto-discovered via my_module_hook_name() naming. Still valid; prefer OOP hooks for new code.

Hooks vs Event Subscribers

Use Hooks WhenUse Event Subscribers When
form_alter, entity hooks, theme hooksreacting to dispatched Symfony events
extending Drupal core/contrib behaviorPSR-14 / decoupled systems
working with existing Drupal APIsbuilding loosely coupled logic

OOP hooks in src/Hook/ are fully testable — instantiate the class directly and inject mocked dependencies.

RULES (IMPORTANT)

  • ALWAYS prefer OOP hooks in src/Hook/ for new implementations
  • NEVER add new hooks in .module unless explicitly requested
  • If procedural hook exists, suggest refactoring to OOP

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.