Updated to Drupal 8.6.4, which is PHP 7.3 friendly. Also updated HTMLaw library....
[yaffs-website] / web / core / modules / content_moderation / src / Plugin / Field / ModerationStateFieldItemList.php
1 <?php
2
3 namespace Drupal\content_moderation\Plugin\Field;
4
5 use Drupal\Core\Entity\ContentEntityInterface;
6 use Drupal\Core\Entity\EntityPublishedInterface;
7 use Drupal\Core\Field\FieldItemList;
8 use Drupal\Core\TypedData\ComputedItemListTrait;
9
10 /**
11  * A computed field that provides a content entity's moderation state.
12  *
13  * It links content entities to a moderation state configuration entity via a
14  * moderation state content entity.
15  */
16 class ModerationStateFieldItemList extends FieldItemList {
17
18   use ComputedItemListTrait {
19     get as traitGet;
20   }
21
22   /**
23    * {@inheritdoc}
24    */
25   protected function computeValue() {
26     $moderation_state = $this->getModerationStateId();
27     // Do not store NULL values, in the case where an entity does not have a
28     // moderation workflow associated with it, we do not create list items for
29     // the computed field.
30     if ($moderation_state) {
31       // An entity can only have a single moderation state.
32       $this->list[0] = $this->createItem(0, $moderation_state);
33     }
34   }
35
36   /**
37    * Gets the moderation state ID linked to a content entity revision.
38    *
39    * @return string|null
40    *   The moderation state ID linked to a content entity revision.
41    */
42   protected function getModerationStateId() {
43     $entity = $this->getEntity();
44
45     /** @var \Drupal\content_moderation\ModerationInformationInterface $moderation_info */
46     $moderation_info = \Drupal::service('content_moderation.moderation_information');
47     if (!$moderation_info->shouldModerateEntitiesOfBundle($entity->getEntityType(), $entity->bundle())) {
48       return NULL;
49     }
50
51     // Existing entities will have a corresponding content_moderation_state
52     // entity associated with them.
53     if (!$entity->isNew() && $content_moderation_state = $this->loadContentModerationStateRevision($entity)) {
54       return $content_moderation_state->moderation_state->value;
55     }
56
57     // It is possible that the bundle does not exist at this point. For example,
58     // the node type form creates a fake Node entity to get default values.
59     // @see \Drupal\node\NodeTypeForm::form()
60     $workflow = $moderation_info->getWorkFlowForEntity($entity);
61     return $workflow ? $workflow->getTypePlugin()->getInitialState($entity)->id() : NULL;
62   }
63
64   /**
65    * Load the content moderation state revision associated with an entity.
66    *
67    * @param \Drupal\Core\Entity\ContentEntityInterface $entity
68    *   The entity the content moderation state entity will be loaded from.
69    *
70    * @return \Drupal\content_moderation\Entity\ContentModerationStateInterface|null
71    *   The content_moderation_state revision or FALSE if none exists.
72    */
73   protected function loadContentModerationStateRevision(ContentEntityInterface $entity) {
74     $moderation_info = \Drupal::service('content_moderation.moderation_information');
75     $content_moderation_storage = \Drupal::entityTypeManager()->getStorage('content_moderation_state');
76
77     $revisions = $content_moderation_storage->getQuery()
78       ->condition('content_entity_type_id', $entity->getEntityTypeId())
79       ->condition('content_entity_id', $entity->id())
80       // Ensure the correct revision is loaded in scenarios where a revision is
81       // being reverted.
82       ->condition('content_entity_revision_id', $entity->isNewRevision() ? $entity->getLoadedRevisionId() : $entity->getRevisionId())
83       ->condition('workflow', $moderation_info->getWorkflowForEntity($entity)->id())
84       ->allRevisions()
85       ->sort('revision_id', 'DESC')
86       ->execute();
87     if (empty($revisions)) {
88       return NULL;
89     }
90
91     /** @var \Drupal\content_moderation\Entity\ContentModerationStateInterface $content_moderation_state */
92     $content_moderation_state = $content_moderation_storage->loadRevision(key($revisions));
93     if ($entity->getEntityType()->hasKey('langcode')) {
94       $langcode = $entity->language()->getId();
95       if (!$content_moderation_state->hasTranslation($langcode)) {
96         $content_moderation_state->addTranslation($langcode);
97       }
98       if ($content_moderation_state->language()->getId() !== $langcode) {
99         $content_moderation_state = $content_moderation_state->getTranslation($langcode);
100       }
101     }
102     return $content_moderation_state;
103   }
104
105   /**
106    * {@inheritdoc}
107    */
108   public function get($index) {
109     if ($index !== 0) {
110       throw new \InvalidArgumentException('An entity can not have multiple moderation states at the same time.');
111     }
112     return $this->traitGet($index);
113   }
114
115   /**
116    * {@inheritdoc}
117    */
118   public function onChange($delta) {
119     $this->updateModeratedEntity($this->list[$delta]->value);
120
121     parent::onChange($delta);
122   }
123
124   /**
125    * {@inheritdoc}
126    */
127   public function setValue($values, $notify = TRUE) {
128     parent::setValue($values, $notify);
129     $this->valueComputed = TRUE;
130
131     // If the parent created a field item and if the parent should be notified
132     // about the change (e.g. this is not initialized with the current value),
133     // update the moderated entity.
134     if (isset($this->list[0]) && $notify) {
135       $this->updateModeratedEntity($this->list[0]->value);
136     }
137   }
138
139   /**
140    * Updates the default revision flag and the publishing status of the entity.
141    *
142    * @param string $moderation_state_id
143    *   The ID of the new moderation state.
144    */
145   protected function updateModeratedEntity($moderation_state_id) {
146     $entity = $this->getEntity();
147
148     /** @var \Drupal\content_moderation\ModerationInformationInterface $content_moderation_info */
149     $content_moderation_info = \Drupal::service('content_moderation.moderation_information');
150     $workflow = $content_moderation_info->getWorkflowForEntity($entity);
151
152     // Change the entity's default revision flag and the publishing status only
153     // if the new workflow state is a valid one.
154     if ($workflow && $workflow->getTypePlugin()->hasState($moderation_state_id)) {
155       /** @var \Drupal\content_moderation\ContentModerationState $current_state */
156       $current_state = $workflow->getTypePlugin()->getState($moderation_state_id);
157
158       // This entity is default if it is new, the default revision state, or the
159       // default revision is not published.
160       $update_default_revision = $entity->isNew()
161         || $current_state->isDefaultRevisionState()
162         || !$content_moderation_info->isDefaultRevisionPublished($entity);
163
164       $entity->isDefaultRevision($update_default_revision);
165
166       // Update publishing status if it can be updated and if it needs updating.
167       $published_state = $current_state->isPublishedState();
168       if (($entity instanceof EntityPublishedInterface) && $entity->isPublished() !== $published_state) {
169         $published_state ? $entity->setPublished() : $entity->setUnpublished();
170       }
171     }
172   }
173
174 }