3 namespace Drupal\content_moderation\Plugin\Action;
5 use Drupal\Core\Access\AccessResult;
6 use Drupal\Core\Action\Plugin\Action\UnpublishAction;
7 use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
8 use Drupal\Core\Entity\EntityTypeManagerInterface;
9 use Drupal\Core\Messenger\MessengerInterface;
10 use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
11 use Drupal\Core\Session\AccountInterface;
12 use Drupal\content_moderation\ModerationInformationInterface;
13 use Symfony\Component\DependencyInjection\ContainerInterface;
16 * Alternate action plugin that can opt-out of modifying moderated entities.
18 * @see \Drupal\Core\Action\Plugin\Action\UnpublishAction
20 class ModerationOptOutUnpublish extends UnpublishAction implements ContainerFactoryPluginInterface {
23 * Moderation information service.
25 * @var \Drupal\content_moderation\ModerationInformationInterface
27 protected $moderationInfo;
30 * Bundle info service.
32 * @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface
34 protected $bundleInfo;
39 * @var \Drupal\Core\Messenger\MessengerInterface
44 * ModerationOptOutUnpublish constructor.
46 * @param array $configuration
47 * A configuration array containing information about the plugin instance.
48 * @param string $plugin_id
49 * The plugin_id for the plugin instance.
50 * @param mixed $plugin_definition
51 * The plugin implementation definition.
52 * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
53 * The entity type manager.
54 * @param \Drupal\content_moderation\ModerationInformationInterface $moderation_info
55 * The moderation information service.
56 * @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $bundle_info
57 * Bundle info service.
58 * @param \Drupal\Core\Messenger\MessengerInterface $messenger
61 public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, ModerationInformationInterface $moderation_info, EntityTypeBundleInfoInterface $bundle_info, MessengerInterface $messenger) {
62 parent::__construct($configuration, $plugin_id, $plugin_definition, $entity_type_manager);
63 $this->moderationInfo = $moderation_info;
64 $this->bundleInfo = $bundle_info;
65 $this->messenger = $messenger;
71 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
73 $configuration, $plugin_id, $plugin_definition,
74 $container->get('entity_type.manager'),
75 $container->get('content_moderation.moderation_information'),
76 $container->get('entity_type.bundle.info'),
77 $container->get('messenger')
84 public function access($entity, AccountInterface $account = NULL, $return_as_object = FALSE) {
85 /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
86 if ($entity && $this->moderationInfo->isModeratedEntity($entity)) {
87 $bundle_info = $this->bundleInfo->getBundleInfo($entity->getEntityTypeId());
88 $bundle_label = $bundle_info[$entity->bundle()]['label'];
89 $this->messenger->addWarning($this->t("@bundle @label were skipped as they are under moderation and may not be directly unpublished.", [
90 '@bundle' => $bundle_label,
91 '@label' => $entity->getEntityType()->getPluralLabel(),
93 $result = AccessResult::forbidden('Cannot directly unpublish moderated entities.');
94 return $return_as_object ? $result : $result->isAllowed();
96 return parent::access($entity, $account, $return_as_object);