SubstitutionManager.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. namespace Drupal\linkit;
  3. use Drupal\Core\Cache\CacheBackendInterface;
  4. use Drupal\Core\Entity\EntityTypeInterface;
  5. use Drupal\Core\Entity\EntityTypeManagerInterface;
  6. use Drupal\Core\Extension\ModuleHandlerInterface;
  7. use Drupal\Core\Plugin\DefaultPluginManager;
  8. /**
  9. * A plugin manager for the substitution plugins.
  10. */
  11. class SubstitutionManager extends DefaultPluginManager implements SubstitutionManagerInterface {
  12. /**
  13. * The entity type manager.
  14. *
  15. * @var \Drupal\Core\Entity\EntityTypeManagerInterface
  16. */
  17. protected $entityTypeManager;
  18. /**
  19. * Constructs the SubstitutionManager object.
  20. *
  21. * @param \Traversable $namespaces
  22. * An object that implements \Traversable which contains the root paths
  23. * keyed by the corresponding namespace to look for plugin implementations.
  24. * @param \Drupal\Core\Cache\CacheBackendInterface $cache_backend
  25. * Cache backend instance to use.
  26. * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
  27. * The module handler to invoke the alter hook with.
  28. * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
  29. * The entity type manager.
  30. */
  31. public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler, EntityTypeManagerInterface $entity_type_manager) {
  32. parent::__construct('Plugin/Linkit/Substitution', $namespaces, $module_handler, 'Drupal\linkit\SubstitutionInterface', 'Drupal\linkit\Annotation\Substitution');
  33. $this->alterInfo('linkit_substitution');
  34. $this->setCacheBackend($cache_backend, 'linkit_substitution');
  35. $this->entityTypeManager = $entity_type_manager;
  36. }
  37. /**
  38. * {@inheritdoc}
  39. */
  40. public function getApplicablePluginsOptionList($entity_type_id) {
  41. $entity_type = $this->entityTypeManager->getDefinition($entity_type_id);
  42. $options = [];
  43. foreach ($this->filterPlugins($this->getDefinitions(), $entity_type) as $id => $definition) {
  44. $options[$id] = $definition['label'];
  45. }
  46. return $options;
  47. }
  48. /**
  49. * Filter the list of plugins by their applicability.
  50. *
  51. * @param array $definitions
  52. * An array of plugin definitions.
  53. * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
  54. * The entity type to get applicable plugins for.
  55. *
  56. * @return array
  57. * The definitions appropriate for the given entity type.
  58. *
  59. * @see SubstitutionInterface::isApplicable()
  60. */
  61. protected function filterPlugins(array $definitions, EntityTypeInterface $entity_type) {
  62. return array_filter($definitions, function ($definition) use ($entity_type) {
  63. /** @var \Drupal\linkit\SubstitutionInterface $class */
  64. $class = $definition['class'];
  65. return $class::isApplicable($entity_type);
  66. });
  67. }
  68. }