class HookDependsOnModule

Same name and namespace in other branches
  1. 11.x core/lib/Drupal/Core/Hook/Attribute/HookDependsOnModule.php \Drupal\Core\Hook\Attribute\HookDependsOnModule

Makes hook registration dependent on a module being installed.

If this attribute is added to a hook class or method, that class or method will be skipped from registration when the module is not installed.

Any module can be set; it does not need to be the module that invokes the hook.

Apart from performance benefits, when this attribute is applied to a class, services provided by the module dependency can be injected as required properties.

Attributes

#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)]

Hierarchy

Expanded class hierarchy of HookDependsOnModule

See also

\Drupal\node\Hook\NodeSearchHooks

6 files declare their use of HookDependsOnModule
DependsOnModuleClassHooks.php in core/modules/system/tests/modules/hook_collector_depends_test/src/Hook/DependsOnModuleClassHooks.php
DependsOnModuleMethodHooks.php in core/modules/system/tests/modules/hook_collector_depends_test/src/Hook/DependsOnModuleMethodHooks.php
DependsOnModuleMultipleHooks.php in core/modules/system/tests/modules/hook_collector_depends_test/src/Hook/DependsOnModuleMultipleHooks.php
FileViewsHooks.php in core/modules/file/src/Hook/FileViewsHooks.php
HookCollectorPass.php in core/lib/Drupal/Core/Hook/HookCollectorPass.php

... See full list

File

core/lib/Drupal/Core/Hook/Attribute/HookDependsOnModule.php, line 22

Namespace

Drupal\Core\Hook\Attribute
View source
class HookDependsOnModule implements HookAttributeInterface {
  
  /**
   * Constructs HookDependsOnModule attribute object.
   *
   * @param string $module
   *   The name of the module that the hook depends on.
   */
  public function __construct(public readonly string $module) {
  }

}

Members

Title Sort descending Modifiers Object type Summary
HookDependsOnModule::__construct public function Constructs HookDependsOnModule attribute object.

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