ModerationStateFieldItemList.php 6.58 KB
Newer Older
1
2
3
4
<?php

namespace Drupal\content_moderation\Plugin\Field;

5
use Drupal\Core\Entity\ContentEntityInterface;
6
use Drupal\Core\Entity\EntityPublishedInterface;
7
use Drupal\Core\Field\FieldItemList;
8
9
10
11
12
13
14

/**
 * A computed field that provides a content entity's moderation state.
 *
 * It links content entities to a moderation state configuration entity via a
 * moderation state content entity.
 */
15
class ModerationStateFieldItemList extends FieldItemList {
16
17

  /**
18
   * Gets the moderation state ID linked to a content entity revision.
19
   *
20
   * @return string|null
21
   *   The moderation state ID linked to a content entity revision.
22
   */
23
  protected function getModerationStateId() {
24
25
    $entity = $this->getEntity();

26
27
28
    /** @var \Drupal\content_moderation\ModerationInformationInterface $moderation_info */
    $moderation_info = \Drupal::service('content_moderation.moderation_information');
    if (!$moderation_info->shouldModerateEntitiesOfBundle($entity->getEntityType(), $entity->bundle())) {
29
30
31
      return NULL;
    }

32
33
34
35
    // Existing entities will have a corresponding content_moderation_state
    // entity associated with them.
    if (!$entity->isNew() && $content_moderation_state = $this->loadContentModerationStateRevision($entity)) {
      return $content_moderation_state->moderation_state->value;
36
    }
37

38
39
40
    // It is possible that the bundle does not exist at this point. For example,
    // the node type form creates a fake Node entity to get default values.
    // @see \Drupal\node\NodeTypeForm::form()
41
    $workflow = $moderation_info->getWorkFlowForEntity($entity);
42
    return $workflow ? $workflow->getTypePlugin()->getInitialState($entity)->id() : NULL;
43
44
45
46
47
48
49
50
  }

  /**
   * Load the content moderation state revision associated with an entity.
   *
   * @param \Drupal\Core\Entity\ContentEntityInterface $entity
   *   The entity the content moderation state entity will be loaded from.
   *
51
   * @return \Drupal\content_moderation\Entity\ContentModerationStateInterface|null
52
53
54
55
56
57
58
59
60
61
62
63
   *   The content_moderation_state revision or FALSE if none exists.
   */
  protected function loadContentModerationStateRevision(ContentEntityInterface $entity) {
    $moderation_info = \Drupal::service('content_moderation.moderation_information');
    $content_moderation_storage = \Drupal::entityTypeManager()->getStorage('content_moderation_state');

    $revisions = \Drupal::service('entity.query')->get('content_moderation_state')
      ->condition('content_entity_type_id', $entity->getEntityTypeId())
      ->condition('content_entity_id', $entity->id())
      // Ensure the correct revision is loaded in scenarios where a revision is
      // being reverted.
      ->condition('content_entity_revision_id', $entity->isNewRevision() ? $entity->getLoadedRevisionId() : $entity->getRevisionId())
64
      ->condition('workflow', $moderation_info->getWorkflowForEntity($entity)->id())
65
66
67
68
69
70
71
      ->allRevisions()
      ->sort('revision_id', 'DESC')
      ->execute();
    if (empty($revisions)) {
      return NULL;
    }

72
    /** @var \Drupal\content_moderation\Entity\ContentModerationStateInterface $content_moderation_state */
73
74
75
76
77
78
79
80
81
    $content_moderation_state = $content_moderation_storage->loadRevision(key($revisions));
    if ($entity->getEntityType()->hasKey('langcode')) {
      $langcode = $entity->language()->getId();
      if (!$content_moderation_state->hasTranslation($langcode)) {
        $content_moderation_state->addTranslation($langcode);
      }
      if ($content_moderation_state->language()->getId() !== $langcode) {
        $content_moderation_state = $content_moderation_state->getTranslation($langcode);
      }
82
    }
83
    return $content_moderation_state;
84
85
86
87
88
89
90
91
92
  }

  /**
   * {@inheritdoc}
   */
  public function get($index) {
    if ($index !== 0) {
      throw new \InvalidArgumentException('An entity can not have multiple moderation states at the same time.');
    }
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
    $this->computeModerationFieldItemList();
    return isset($this->list[$index]) ? $this->list[$index] : NULL;
  }

  /**
   * {@inheritdoc}
   */
  public function getIterator() {
    $this->computeModerationFieldItemList();
    return parent::getIterator();
  }

  /**
   * Recalculate the moderation field item list.
   */
  protected function computeModerationFieldItemList() {
109
    // Compute the value of the moderation state.
110
    $index = 0;
111
    if (!isset($this->list[$index]) || $this->list[$index]->isEmpty()) {
112

113
      $moderation_state = $this->getModerationStateId();
114
115
      // Do not store NULL values in the static cache.
      if ($moderation_state) {
116
        $this->list[$index] = $this->createItem($index, $moderation_state);
117
118
119
120
      }
    }
  }

121
122
123
124
  /**
   * {@inheritdoc}
   */
  public function onChange($delta) {
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
    $this->updateModeratedEntity($this->list[$delta]->value);

    parent::onChange($delta);
  }

  /**
   * {@inheritdoc}
   */
  public function setValue($values, $notify = TRUE) {
    parent::setValue($values, $notify);

    if (isset($this->list[0])) {
      $this->updateModeratedEntity($this->list[0]->value);
    }
  }

  /**
   * Updates the default revision flag and the publishing status of the entity.
   *
   * @param string $moderation_state_id
   *   The ID of the new moderation state.
   */
  protected function updateModeratedEntity($moderation_state_id) {
148
149
150
151
152
153
154
155
    $entity = $this->getEntity();

    /** @var \Drupal\content_moderation\ModerationInformationInterface $content_moderation_info */
    $content_moderation_info = \Drupal::service('content_moderation.moderation_information');
    $workflow = $content_moderation_info->getWorkflowForEntity($entity);

    // Change the entity's default revision flag and the publishing status only
    // if the new workflow state is a valid one.
156
    if ($workflow && $workflow->getTypePlugin()->hasState($moderation_state_id)) {
157
      /** @var \Drupal\content_moderation\ContentModerationState $current_state */
158
      $current_state = $workflow->getTypePlugin()->getState($moderation_state_id);
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176

      // This entity is default if it is new, a new translation, the default
      // revision state, or the default revision is not published.
      $update_default_revision = $entity->isNew()
        || $entity->isNewTranslation()
        || $current_state->isDefaultRevisionState()
        || !$content_moderation_info->isDefaultRevisionPublished($entity);

      $entity->isDefaultRevision($update_default_revision);

      // Update publishing status if it can be updated and if it needs updating.
      $published_state = $current_state->isPublishedState();
      if (($entity instanceof EntityPublishedInterface) && $entity->isPublished() !== $published_state) {
        $published_state ? $entity->setPublished() : $entity->setUnpublished();
      }
    }
  }

177
}