3 namespace Drupal\Core\Action\Plugin\Action\Derivative;
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;
12 * Provides a base action for each entity type with specific interfaces.
14 abstract class EntityActionDeriverBase extends DeriverBase implements ContainerDeriverInterface {
17 * The entity type manager.
19 * @var \Drupal\Core\Entity\EntityTypeManagerInterface
21 protected $entityTypeManager;
24 * Constructs a new EntityActionDeriverBase object.
26 * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
27 * The entity type manager.
29 public function __construct(EntityTypeManagerInterface $entity_type_manager) {
30 $this->entityTypeManager = $entity_type_manager;
36 public static function create(ContainerInterface $container, $base_plugin_id) {
37 return new static($container->get('entity_type.manager'));
41 * Indicates whether the deriver can be used for the provided entity type.
43 * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
47 * TRUE if the entity type can be used, FALSE otherwise.
49 abstract protected function isApplicable(EntityTypeInterface $entity_type);
54 public function getDerivativeDefinitions($base_plugin_definition) {
55 if (empty($this->derivatives)) {
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;
63 $this->derivatives = $definitions;
66 return parent::getDerivativeDefinitions($base_plugin_definition);
70 * Gets a list of applicable entity types.
72 * The list consists of all entity types which match the conditions for the
74 * For example, if the action applies to entities that are publishable,
75 * this method will find all entity types that are publishable.
77 * @return \Drupal\Core\Entity\EntityTypeInterface[]
78 * The applicable entity types, keyed by entity type ID.
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);