Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / content_moderation / src / Plugin / Action / ModerationOptOutUnpublish.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\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;
14
15 /**
16  * Alternate action plugin that can opt-out of modifying moderated entities.
17  *
18  * @see \Drupal\Core\Action\Plugin\Action\UnpublishAction
19  */
20 class ModerationOptOutUnpublish extends UnpublishAction implements ContainerFactoryPluginInterface {
21
22   /**
23    * Moderation information service.
24    *
25    * @var \Drupal\content_moderation\ModerationInformationInterface
26    */
27   protected $moderationInfo;
28
29   /**
30    * Bundle info service.
31    *
32    * @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface
33    */
34   protected $bundleInfo;
35
36   /**
37    * Messenger service.
38    *
39    * @var \Drupal\Core\Messenger\MessengerInterface
40    */
41   protected $messenger;
42
43   /**
44    * ModerationOptOutUnpublish constructor.
45    *
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
59    *   Messenger service.
60    */
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;
66   }
67
68   /**
69    * {@inheritdoc}
70    */
71   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
72     return new static(
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')
78     );
79   }
80
81   /**
82    * {@inheritdoc}
83    */
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(),
92       ]));
93       $result = AccessResult::forbidden('Cannot directly unpublish moderated entities.');
94       return $return_as_object ? $result : $result->isAllowed();
95     }
96     return parent::access($entity, $account, $return_as_object);
97   }
98
99 }