ModerationStateFieldItemList.php 3.71 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
<?php

namespace Drupal\content_moderation\Plugin\Field;

use Drupal\content_moderation\Entity\ModerationState;
use Drupal\Core\Field\EntityReferenceFieldItemList;

/**
 * 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.
 */
class ModerationStateFieldItemList extends EntityReferenceFieldItemList {

  /**
   * Gets the moderation state entity linked to a content entity revision.
   *
   * @return \Drupal\content_moderation\ModerationStateInterface|null
   *   The moderation state configuration entity linked to a content entity
   *   revision.
   */
  protected function getModerationState() {
    $entity = $this->getEntity();

26 27 28 29
    if (!\Drupal::service('content_moderation.moderation_information')->shouldModerateEntitiesOfBundle($entity->getEntityType(), $entity->bundle())) {
      return NULL;
    }

30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
    if ($entity->id() && $entity->getRevisionId()) {
      $revisions = \Drupal::service('entity.query')->get('content_moderation_state')
        ->condition('content_entity_type_id', $entity->getEntityTypeId())
        ->condition('content_entity_id', $entity->id())
        ->condition('content_entity_revision_id', $entity->getRevisionId())
        ->allRevisions()
        ->sort('revision_id', 'DESC')
        ->execute();

      if ($revision_to_load = key($revisions)) {
        /** @var \Drupal\content_moderation\ContentModerationStateInterface $content_moderation_state */
        $content_moderation_state = \Drupal::entityTypeManager()
          ->getStorage('content_moderation_state')
          ->loadRevision($revision_to_load);

        // Return the correct translation.
46 47 48 49 50 51 52 53
        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);
          }
54 55 56 57 58 59 60 61
        }

        return $content_moderation_state->get('moderation_state')->entity;
      }
    }
    // 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()
62 63 64
    $bundle_entity = \Drupal::entityTypeManager()
      ->getStorage($entity->getEntityType()->getBundleEntityType())
      ->load($entity->bundle());
65 66 67 68 69 70 71 72 73 74 75 76
    if ($bundle_entity && ($default = $bundle_entity->getThirdPartySetting('content_moderation', 'default_moderation_state'))) {
      return ModerationState::load($default);
    }
  }

  /**
   * {@inheritdoc}
   */
  public function get($index) {
    if ($index !== 0) {
      throw new \InvalidArgumentException('An entity can not have multiple moderation states at the same time.');
    }
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
    $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() {
93
    // Compute the value of the moderation state.
94
    $index = 0;
95 96 97 98 99 100 101 102 103 104
    if (!isset($this->list[$index]) || $this->list[$index]->isEmpty()) {
      $moderation_state = $this->getModerationState();
      // Do not store NULL values in the static cache.
      if ($moderation_state) {
        $this->list[$index] = $this->createItem($index, ['entity' => $moderation_state]);
      }
    }
  }

}