class HookDiscovery

Same name and namespace in other branches
  1. 9 core/lib/Drupal/Core/Plugin/Discovery/HookDiscovery.php \Drupal\Core\Plugin\Discovery\HookDiscovery
  2. 8.9.x core/lib/Drupal/Core/Plugin/Discovery/HookDiscovery.php \Drupal\Core\Plugin\Discovery\HookDiscovery
  3. 11.x core/lib/Drupal/Core/Plugin/Discovery/HookDiscovery.php \Drupal\Core\Plugin\Discovery\HookDiscovery

Provides a hook-based plugin discovery class.

Hierarchy

Expanded class hierarchy of HookDiscovery

1 file declares its use of HookDiscovery
HookDiscoveryTest.php in core/tests/Drupal/Tests/Core/Plugin/Discovery/HookDiscoveryTest.php

File

core/lib/Drupal/Core/Plugin/Discovery/HookDiscovery.php, line 12

Namespace

Drupal\Core\Plugin\Discovery
View source
class HookDiscovery implements DiscoveryInterface {
    use DiscoveryTrait;
    
    /**
     * The name of the hook that will be implemented by this discovery instance.
     *
     * @var string
     */
    protected $hook;
    
    /**
     * The module handler used to find and execute the plugin hook.
     *
     * @var \Drupal\Core\Extension\ModuleHandlerInterface
     */
    protected $moduleHandler;
    
    /**
     * Constructs a Drupal\Core\Plugin\Discovery\HookDiscovery object.
     *
     * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
     *   The module handler.
     * @param string $hook
     *   The Drupal hook that a module can implement in order to interface to
     *   this discovery class.
     */
    public function __construct(ModuleHandlerInterface $module_handler, $hook) {
        $this->moduleHandler = $module_handler;
        $this->hook = $hook;
    }
    
    /**
     * {@inheritdoc}
     */
    public function getDefinitions() {
        $definitions = [];
        $this->moduleHandler
            ->invokeAllWith($this->hook, function (callable $hook, string $module) use (&$definitions) {
            $module_definitions = $hook();
            foreach ($module_definitions as $plugin_id => $definition) {
                $definition['provider'] = $module;
                $definitions[$plugin_id] = $definition;
            }
        });
        return $definitions;
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
DiscoveryTrait::doGetDefinition protected function Gets a specific plugin definition.
DiscoveryTrait::getDefinition public function 3
DiscoveryTrait::hasDefinition public function
HookDiscovery::$hook protected property The name of the hook that will be implemented by this discovery instance.
HookDiscovery::$moduleHandler protected property The module handler used to find and execute the plugin hook.
HookDiscovery::getDefinitions public function Gets the definition of all plugins for this type. Overrides DiscoveryTrait::getDefinitions
HookDiscovery::__construct public function Constructs a Drupal\Core\Plugin\Discovery\HookDiscovery object.

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