3 namespace Drupal\content_moderation\Plugin\Action;
5 use Drupal\Core\Access\AccessResult;
6 use Drupal\Core\Action\Plugin\Action\PublishAction;
7 use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
8 use Drupal\Core\Entity\EntityTypeManagerInterface;
9 use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
10 use Drupal\Core\Session\AccountInterface;
11 use Drupal\content_moderation\ModerationInformationInterface;
12 use Symfony\Component\DependencyInjection\ContainerInterface;
15 * Alternate action plugin that can opt-out of modifying moderated entities.
17 * @see \Drupal\Core\Action\Plugin\Action\PublishAction
19 class ModerationOptOutPublish extends PublishAction implements ContainerFactoryPluginInterface {
22 * Moderation information service.
24 * @var \Drupal\content_moderation\ModerationInformationInterface
26 protected $moderationInfo;
29 * Bundle info service.
31 * @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface
33 protected $bundleInfo;
36 * ModerationOptOutPublish constructor.
38 * @param array $configuration
39 * A configuration array containing information about the plugin instance.
40 * @param string $plugin_id
41 * The plugin_id for the plugin instance.
42 * @param mixed $plugin_definition
43 * The plugin implementation definition.
44 * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
45 * The entity type manager.
46 * @param \Drupal\content_moderation\ModerationInformationInterface $moderation_info
47 * The moderation information service.
48 * @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $bundle_info
49 * Bundle info service.
51 public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, ModerationInformationInterface $moderation_info, EntityTypeBundleInfoInterface $bundle_info) {
52 parent::__construct($configuration, $plugin_id, $plugin_definition, $entity_type_manager);
53 $this->moderationInfo = $moderation_info;
54 $this->bundleInfo = $bundle_info;
60 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
62 $configuration, $plugin_id, $plugin_definition,
63 $container->get('entity_type.manager'),
64 $container->get('content_moderation.moderation_information'),
65 $container->get('entity_type.bundle.info')
72 public function access($entity, AccountInterface $account = NULL, $return_as_object = FALSE) {
73 /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
74 if ($entity && $this->moderationInfo->isModeratedEntity($entity)) {
75 $bundle_info = $this->bundleInfo->getBundleInfo($entity->getEntityTypeId());
76 $bundle_label = $bundle_info[$entity->bundle()]['label'];
77 drupal_set_message($this->t("@bundle @label were skipped as they are under moderation and may not be directly published.", ['@bundle' => $bundle_label, '@label' => $entity->getEntityType()->getPluralLabel()]), 'warning');
78 $result = AccessResult::forbidden();
79 return $return_as_object ? $result : $result->isAllowed();
81 return parent::access($entity, $account, $return_as_object);