HookDiscovery.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php
  2. namespace Drupal\Core\Plugin\Discovery;
  3. use Drupal\Component\Plugin\Discovery\DiscoveryInterface;
  4. use Drupal\Component\Plugin\Discovery\DiscoveryTrait;
  5. use Drupal\Core\Extension\ModuleHandlerInterface;
  6. /**
  7. * Provides a hook-based plugin discovery class.
  8. */
  9. class HookDiscovery implements DiscoveryInterface {
  10. use DiscoveryTrait;
  11. /**
  12. * The name of the hook that will be implemented by this discovery instance.
  13. *
  14. * @var string
  15. */
  16. protected $hook;
  17. /**
  18. * The module handler used to find and execute the plugin hook.
  19. *
  20. * @var \Drupal\Core\Extension\ModuleHandlerInterface
  21. */
  22. protected $moduleHandler;
  23. /**
  24. * Constructs a Drupal\Core\Plugin\Discovery\HookDiscovery object.
  25. *
  26. * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
  27. * The module handler.
  28. * @param string $hook
  29. * The Drupal hook that a module can implement in order to interface to
  30. * this discovery class.
  31. */
  32. public function __construct(ModuleHandlerInterface $module_handler, $hook) {
  33. $this->moduleHandler = $module_handler;
  34. $this->hook = $hook;
  35. }
  36. /**
  37. * {@inheritdoc}
  38. */
  39. public function getDefinitions() {
  40. $definitions = [];
  41. foreach ($this->moduleHandler->getImplementations($this->hook) as $module) {
  42. $result = $this->moduleHandler->invoke($module, $this->hook);
  43. foreach ($result as $plugin_id => $definition) {
  44. $definition['provider'] = $module;
  45. $definitions[$plugin_id] = $definition;
  46. }
  47. }
  48. return $definitions;
  49. }
  50. }