6a707e852fdcb844e50e1b650bc8a06ce15997b7
[yaffs-website] / web / core / modules / content_moderation / src / Plugin / Action / ModerationOptOutPublish.php
1 <?php
2
3 namespace Drupal\content_moderation\Plugin\Action;
4
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;
13
14 /**
15  * Alternate action plugin that can opt-out of modifying moderated entities.
16  *
17  * @see \Drupal\Core\Action\Plugin\Action\PublishAction
18  */
19 class ModerationOptOutPublish extends PublishAction implements ContainerFactoryPluginInterface {
20
21   /**
22    * Moderation information service.
23    *
24    * @var \Drupal\content_moderation\ModerationInformationInterface
25    */
26   protected $moderationInfo;
27
28   /**
29    * Bundle info service.
30    *
31    * @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface
32    */
33   protected $bundleInfo;
34
35   /**
36    * ModerationOptOutPublish constructor.
37    *
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.
50    */
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;
55   }
56
57   /**
58    * {@inheritdoc}
59    */
60   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
61     return new static(
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')
66     );
67   }
68
69   /**
70    * {@inheritdoc}
71    */
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();
80     }
81     return parent::access($entity, $account, $return_as_object);
82   }
83
84 }