class Hook

Same name in other branches
  1. 10 core/lib/Drupal/Core/Hook/Attribute/Hook.php \Drupal\Core\Hook\Attribute\Hook

Attribute for defining a class method as a hook implementation.

Hook implementations in classes need to be marked with this attribute, using one of the following techniques:

  • On a method, use this attribute with the hook name:

  #[Hook('user_cancel')]
  public method userCancel(...)
  
  • On a class, specifying the method name:

  #[Hook('user_cancel', method: 'userCancel')]
  class Hooks {
    method userCancel(...) {}
  }
  
  • On a class with an __invoke method, which is taken to be the hook implementation:

  #[Hook('user_cancel')]
  class Hooks {
    method __invoke(...) {}
  }
  

Ordering hook implementations can be done by implementing hook_module_implements_alter.

Classes that use this annotation on the class or on their methods are automatically registered as autowired services with the class name as the service ID. If autowire does not suffice, they can be registered manually as well.

Implementing a hook on behalf of another module can be done by by specifying the 'module' parameter in the attribute.

Multiple implementations

Multiple implementations are allowed on multiple axes:

  • One method can implement multiple hooks by adding a Hook attribute for each method.
  • One module can implement a particular hook multiple times in multiple classes, although see below for some exceptions. This allows, for example, adding hook_form_alter() implementations firing on other conditions than form ID without modifying any existing implementations.

The following hooks may not have multiple implementations by a single module:

Procedural hooks

The following hooks can only have procedural hook implementations:

Legacy meta hooks:

Install hooks:

Theme hooks:

Backwards-compatibility

To allow hook implementations to work on older versions of Drupal as well, add both an attribute-based hook implementation and a procedural hook implementation, with the \Drupal\Core\Hook\Attribute\LegacyHook attribute on the procedural hook implementations.

See \Drupal\Core\Hook\Attribute\LegacyHook for additional information.

Hierarchy

  • class \Drupal\Core\Hook\Attribute\Hook

Expanded class hierarchy of Hook

269 files declare their use of Hook
AdvisoryFeedTestHooks.php in core/modules/system/tests/modules/advisory_feed_test/src/Hook/AdvisoryFeedTestHooks.php
AnnouncementsFeedHooks.php in core/modules/announcements_feed/src/Hook/AnnouncementsFeedHooks.php
AutomatedCronHooks.php in core/modules/automated_cron/src/Hook/AutomatedCronHooks.php
BanHooks.php in core/modules/ban/src/Hook/BanHooks.php
BasicAuthHooks.php in core/modules/basic_auth/src/Hook/BasicAuthHooks.php

... See full list

4 string references to 'Hook'
drupal7.php in core/modules/migrate_drupal/tests/fixtures/drupal7.php
A database agnostic dump for testing purposes.
ModuleHandlerTest::testImplementsHookModuleEnabled in core/tests/Drupal/Tests/Core/Extension/ModuleHandlerTest.php
Tests implementations methods when module is enabled.
ModuleHandlerTest::testLoadAllIncludes in core/tests/Drupal/Tests/Core/Extension/ModuleHandlerTest.php
@covers ::loadAllIncludes
module_handler_test_hook_info in core/tests/Drupal/Tests/Core/Extension/modules/module_handler_test/module_handler_test.module
Implements hook_hook_info().

File

core/lib/Drupal/Core/Hook/Attribute/Hook.php, line 90

Namespace

Drupal\Core\Hook\Attribute
View source
class Hook {
    
    /**
     * Constructs a Hook attribute object.
     *
     * @param string $hook
     *   The short hook name, without the 'hook_' prefix.
     * @param string $method
     *   (optional) The method name. If this attribute is on a method, this
     *   parameter is not required. If this attribute is on a class and this
     *   parameter is omitted, the class must have an __invoke() method, which is
     *   taken as the hook implementation.
     * @param string|null $module
     *   (optional) The module this implementation is for. This allows one module to
     *   implement a hook on behalf of another module. Defaults to the module the
     *   implementation is in.
     */
    public function __construct(string $hook, string $method = '', ?string $module = NULL) {
    }
    
    /**
     * Set the method the hook should apply to.
     *
     * @param string $method
     *   The method that the hook attribute applies to.
     *   This only needs to be set when the attribute is on the class.
     */
    public function setMethod(string $method) : static {
        $this->method = $method;
        return $this;
    }

}

Members

Title Sort descending Modifiers Object type Summary
Hook::setMethod public function Set the method the hook should apply to.
Hook::__construct public function Constructs a Hook attribute object.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.