29159c2931115cde69ed587dd81b4ccb0a389a17
[yaffs-website] / web / core / lib / Drupal / Core / Action / Plugin / Action / Derivative / EntityActionDeriverBase.php
1 <?php
2
3 namespace Drupal\Core\Action\Plugin\Action\Derivative;
4
5 use Drupal\Component\Plugin\Derivative\DeriverBase;
6 use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface;
7 use Drupal\Core\Entity\EntityTypeInterface;
8 use Drupal\Core\Entity\EntityTypeManagerInterface;
9 use Symfony\Component\DependencyInjection\ContainerInterface;
10
11 /**
12  * Provides a base action for each entity type with specific interfaces.
13  */
14 abstract class EntityActionDeriverBase extends DeriverBase implements ContainerDeriverInterface {
15
16   /**
17    * The entity type manager.
18    *
19    * @var \Drupal\Core\Entity\EntityTypeManagerInterface
20    */
21   protected $entityTypeManager;
22
23   /**
24    * Constructs a new EntityActionDeriverBase object.
25    *
26    * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
27    *   The entity type manager.
28    */
29   public function __construct(EntityTypeManagerInterface $entity_type_manager) {
30     $this->entityTypeManager = $entity_type_manager;
31   }
32
33   /**
34    * {@inheritdoc}
35    */
36   public static function create(ContainerInterface $container, $base_plugin_id) {
37     return new static($container->get('entity_type.manager'));
38   }
39
40   /**
41    * Indicates whether the deriver can be used for the provided entity type.
42    *
43    * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
44    *   The entity type.
45    *
46    * @return bool
47    *   TRUE if the entity type can be used, FALSE otherwise.
48    */
49   abstract protected function isApplicable(EntityTypeInterface $entity_type);
50
51   /**
52    * {@inheritdoc}
53    */
54   public function getDerivativeDefinitions($base_plugin_definition) {
55     if (empty($this->derivatives)) {
56       $definitions = [];
57       foreach ($this->getApplicableEntityTypes() as $entity_type_id => $entity_type) {
58         $definition = $base_plugin_definition;
59         $definition['type'] = $entity_type_id;
60         $definition['label'] = sprintf('%s %s', $base_plugin_definition['action_label'], $entity_type->getSingularLabel());
61         $definitions[$entity_type_id] = $definition;
62       }
63       $this->derivatives = $definitions;
64     }
65
66     return parent::getDerivativeDefinitions($base_plugin_definition);
67   }
68
69   /**
70    * Gets a list of applicable entity types.
71    *
72    * The list consists of all entity types which match the conditions for the
73    * given deriver.
74    * For example, if the action applies to entities that are publishable,
75    * this method will find all entity types that are publishable.
76    *
77    * @return \Drupal\Core\Entity\EntityTypeInterface[]
78    *   The applicable entity types, keyed by entity type ID.
79    */
80   protected function getApplicableEntityTypes() {
81     $entity_types = $this->entityTypeManager->getDefinitions();
82     $entity_types = array_filter($entity_types, function (EntityTypeInterface $entity_type) {
83       return $this->isApplicable($entity_type);
84     });
85
86     return $entity_types;
87   }
88
89 }