HookDiscovery.php 1.65 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
<?php

/**
 * @file
 * Definition of Drupal\Core\Plugin\Discovery\HookDiscovery.
 */

namespace Drupal\Core\Plugin\Discovery;

use Drupal\Component\Plugin\Discovery\DiscoveryInterface;
11
use Drupal\Component\Plugin\Discovery\DiscoveryTrait;
12
use Drupal\Core\Extension\ModuleHandlerInterface;
13
14
15
16
17
18

/**
 * Provides a hook-based plugin discovery class.
 */
class HookDiscovery implements DiscoveryInterface {

19
20
  use DiscoveryTrait;

21
22
23
24
25
26
27
  /**
   * The name of the hook that will be implemented by this discovery instance.
   *
   * @var string
   */
  protected $hook;

28
29
30
31
32
33
34
  /**
   * The module handler used to find and execute the plugin hook.
   *
   * @var \Drupal\Core\Extension\ModuleHandlerInterface
   */
  protected $moduleHandler;

35
36
37
  /**
   * Constructs a Drupal\Core\Plugin\Discovery\HookDiscovery object.
   *
38
39
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
   *   The module handler.
40
41
42
43
   * @param string $hook
   *   The Drupal hook that a module can implement in order to interface to
   *   this discovery class.
   */
44
45
  function __construct(ModuleHandlerInterface $module_handler, $hook) {
    $this->moduleHandler = $module_handler;
46
47
48
49
    $this->hook = $hook;
  }

  /**
50
   * Implements Drupal\Component\Plugin\Discovery\DiscoveryInterface::getDefinitions().
51
52
   */
  public function getDefinitions() {
53
    $definitions = array();
54
55
56
    foreach ($this->moduleHandler->getImplementations($this->hook) as $module) {
      $result = $this->moduleHandler->invoke($module, $this->hook);
      foreach ($result as $plugin_id => $definition) {
57
58
59
60
61
62
63
        $definition['module'] = $module;
        $definitions[$plugin_id] = $definition;
      }
    }
    return $definitions;
  }
}