3 namespace Drupal\entity\Plugin\Action\Derivative;
5 use Drupal\Component\Plugin\Derivative\DeriverBase;
6 use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface;
7 use Drupal\Core\Entity\ContentEntityInterface;
8 use Drupal\Core\Entity\EntityTypeInterface;
9 use Drupal\Core\Entity\EntityTypeManagerInterface;
10 use Symfony\Component\DependencyInjection\ContainerInterface;
13 * Provides a delete action for each content entity type.
15 class DeleteActionDeriver extends DeriverBase implements ContainerDeriverInterface {
18 * The entity type manager.
20 * @var \Drupal\Core\Entity\EntityManagerInterface
22 protected $entityTypeManager;
25 * Constructs a new DeleteActionDeriver object.
27 * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
28 * The entity type manager.
30 public function __construct(EntityTypeManagerInterface $entity_type_manager) {
31 $this->entityTypeManager = $entity_type_manager;
37 public static function create(ContainerInterface $container, $base_plugin_id) {
38 return new static($container->get('entity_type.manager'));
44 public function getDerivativeDefinitions($base_plugin_definition) {
45 if (empty($this->derivatives)) {
47 foreach ($this->getParticipatingEntityTypes() as $entity_type_id => $entity_type) {
48 $definition = $base_plugin_definition;
49 $definition['label'] = t('Delete @entity_type', ['@entity_type' => $entity_type->getLowercaseLabel()]);
50 $definition['type'] = $entity_type_id;
51 $definition['confirm_form_route_name'] = 'entity.' . $entity_type_id . '.delete_multiple_form';
52 $definitions[$entity_type_id] = $definition;
54 $this->derivatives = $definitions;
57 return parent::getDerivativeDefinitions($base_plugin_definition);
61 * Gets a list of participating entity types.
63 * The list consists of all content entity types with a delete-multiple-form
66 * @return \Drupal\Core\Entity\EntityTypeInterface[]
67 * The participating entity types, keyed by entity type id.
69 protected function getParticipatingEntityTypes() {
70 $entity_types = $this->entityTypeManager->getDefinitions();
71 $entity_types = array_filter($entity_types, function (EntityTypeInterface $entity_type) {
72 return $entity_type->isSubclassOf(ContentEntityInterface::class) && $entity_type->hasLinkTemplate('delete-multiple-form');