You are here

class ModerationInformation in Config Entity Revisions 8.2

Class ModerationInformation.

@package Drupal\config_entity_revisions

Hierarchy

Expanded class hierarchy of ModerationInformation

1 string reference to 'ModerationInformation'
config_entity_revisions.services.yml in ./config_entity_revisions.services.yml
config_entity_revisions.services.yml
1 service uses ModerationInformation
config_entity_revisions.moderation_information in ./config_entity_revisions.services.yml
Drupal\config_entity_revisions\ModerationInformation

File

src/ModerationInformation.php, line 19

Namespace

Drupal\config_entity_revisions
View source
class ModerationInformation extends \Drupal\content_moderation\ModerationInformation implements ModerationInformationInterface {
  use StringTranslationTrait;

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * The bundle information service.
   *
   * @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface
   */
  protected $bundleInfo;

  /**
   * Creates a new ModerationInformation instance.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   * @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $bundle_info
   *   The bundle information service.
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager, EntityTypeBundleInfoInterface $bundle_info) {
    $this->entityTypeManager = $entity_type_manager;
    $this->bundleInfo = $bundle_info;
  }

  /**
   * {@inheritdoc}
   */
  public function isModeratedEntity(EntityInterface $entity) {
    if ($entity instanceof ConfigEntityRevisionsConfigEntityInterface) {
      return $this
        ->shouldModerateConfigEntity($entity);
    }
    return parent::isModeratedEntity($entity);
  }

  /**
   * {@inheritdoc}
   */
  public function canModerateEntitiesOfEntityType(EntityTypeInterface $entity_type) {
    return $entity_type
      ->hasHandlerClass('moderation');
  }

  /**
   * {@inheritdoc}
   */
  public function shouldModerateEntitiesOfBundle(EntityTypeInterface $entity_type, $bundle) {
    if ($this
      ->canModerateEntitiesOfEntityType($entity_type)) {
      $bundles = $this->bundleInfo
        ->getBundleInfo($entity_type
        ->id());
      return isset($bundles[$bundle]['workflow']);
    }
    return FALSE;
  }

  /**
   * Whether this type of config entity has moderation enabled.
   *
   * @param string $entity
   *   The configuration entity.
   *
   * @return bool
   *   Whether the config entity type is moderated.
   */
  public function shouldModerateConfigEntity($entity) {
    if ($this
      ->canModerateEntitiesOfEntityType($entity
      ->getEntityType())) {
      $bundles = $this->bundleInfo
        ->getBundleInfo('config_entity_revisions');
      $key = $entity
        ->getBundleName();
      return isset($bundles[$key]['workflow']);
    }
    return FALSE;
  }

  /**
   * {@inheritdoc}
   */
  public function getLatestRevision($entity_type_id, $entity_id) {
    if ($latest_revision_id = $this
      ->getLatestRevisionId($entity_type_id, $entity_id)) {
      return $this->entityTypeManager
        ->getStorage($entity_type_id)
        ->loadRevision($latest_revision_id);
    }
  }

  /**
   * {@inheritdoc}
   */
  public function getLatestRevisionId($entity_type_id, $entity_id) {
    if ($storage = $this->entityTypeManager
      ->getStorage($entity_type_id)) {
      $result = $storage
        ->getQuery()
        ->latestRevision()
        ->condition($this->entityTypeManager
        ->getDefinition($entity_type_id)
        ->getKey('id'), $entity_id)
        ->accessCheck(FALSE)
        ->execute();
      if ($result) {
        return key($result);
      }
    }
  }

  /**
   * {@inheritdoc}
   */
  public function getDefaultRevisionId($entity_type_id, $entity_id) {
    if ($storage = $this->entityTypeManager
      ->getStorage($entity_type_id)) {
      $result = $storage
        ->getQuery()
        ->currentRevision()
        ->condition($this->entityTypeManager
        ->getDefinition($entity_type_id)
        ->getKey('id'), $entity_id)
        ->accessCheck(FALSE)
        ->execute();
      if ($result) {
        return key($result);
      }
    }
  }

  /**
   * {@inheritdoc}
   */
  public function getAffectedRevisionTranslation(ContentEntityInterface $entity) {
    foreach ($entity
      ->getTranslationLanguages() as $language) {
      $translation = $entity
        ->getTranslation($language
        ->getId());
      if (!$translation
        ->isDefaultRevision() && $translation
        ->isRevisionTranslationAffected()) {
        return $translation;
      }
    }
  }

  /**
   * {@inheritdoc}
   */
  public function isLatestRevision(ContentEntityInterface $entity) {
    return $entity
      ->getRevisionId() == $this
      ->getLatestRevisionId($entity
      ->getEntityTypeId(), $entity
      ->id());
  }

  /**
   * {@inheritdoc}
   */
  public function hasPendingRevision(ContentEntityInterface $entity) {
    $result = FALSE;
    if ($this
      ->isModeratedEntity($entity)) {

      /** @var \Drupal\Core\Entity\ContentEntityStorageInterface $storage */
      $storage = $this->entityTypeManager
        ->getStorage($entity
        ->getEntityTypeId());
      $latest_revision_id = $storage
        ->getLatestTranslationAffectedRevisionId($entity
        ->id(), $entity
        ->language()
        ->getId());
      $default_revision_id = $entity
        ->isDefaultRevision() && !$entity
        ->isNewRevision() && ($revision_id = $entity
        ->getRevisionId()) ? $revision_id : $this
        ->getDefaultRevisionId($entity
        ->getEntityTypeId(), $entity
        ->id());
      if ($latest_revision_id != $default_revision_id) {

        /** @var \Drupal\Core\Entity\ContentEntityInterface $latest_revision */
        $latest_revision = $storage
          ->loadRevision($latest_revision_id);
        $result = !$latest_revision
          ->wasDefaultRevision();
      }
    }
    return $result;
  }

  /**
   * {@inheritdoc}
   */
  public function isLiveRevision(ContentEntityInterface $entity) {
    $workflow = $this
      ->getWorkflowForEntity($entity);
    return $this
      ->isLatestRevision($entity) && $entity
      ->isDefaultRevision() && $entity->moderation_state->value && $workflow
      ->getTypePlugin()
      ->getState($entity->moderation_state->value)
      ->isPublishedState();
  }

  /**
   * {@inheritdoc}
   */
  public function isDefaultRevisionPublished(ContentEntityInterface $entity) {
    $workflow = $this
      ->getWorkflowForEntity($entity);
    $default_revision = \Drupal::entityTypeManager()
      ->getStorage($entity
      ->getEntityTypeId())
      ->load($entity
      ->id());

    // If no default revision could be loaded, the entity has not yet been
    // saved. In this case the moderation_state of the unsaved entity can be
    // used, since once saved it will become the default.
    $default_revision = $default_revision ?: $entity;

    // Ensure we are checking all translations of the default revision.
    if ($default_revision instanceof TranslatableInterface && $default_revision
      ->isTranslatable()) {

      // Loop through each language that has a translation.
      foreach ($default_revision
        ->getTranslationLanguages() as $language) {

        // Load the translated revision.
        $translation = $default_revision
          ->getTranslation($language
          ->getId());

        // If the moderation state is empty, it was not stored yet so no point
        // in doing further work.
        $moderation_state = $translation->moderation_state->value;
        if (!$moderation_state) {
          continue;
        }

        // Return TRUE if a translation with a published state is found.
        if ($workflow
          ->getTypePlugin()
          ->getState($moderation_state)
          ->isPublishedState()) {
          return TRUE;
        }
      }
    }
    return $workflow
      ->getTypePlugin()
      ->getState($default_revision->moderation_state->value)
      ->isPublishedState();
  }

  /**
   * {@inheritdoc}
   */
  public function getWorkflowForEntity(ContentEntityInterface $entity) {
    $bundles = $this->bundleInfo
      ->getBundleInfo($entity
      ->getEntityTypeId());
    if (isset($bundles[$entity
      ->bundle()]['workflow'])) {
      return $this->entityTypeManager
        ->getStorage('workflow')
        ->load($bundles[$entity
        ->bundle()]['workflow']);
    }
    return NULL;
  }

  /**
   * {@inheritdoc}
   */
  public function getUnsupportedFeatures(EntityTypeInterface $entity_type) {
    $features = [];

    // Test if entity is publishable.
    if (!$entity_type
      ->entityClassImplements(EntityPublishedInterface::class)) {
      $features['publishing'] = $this
        ->t("@entity_type_plural_label do not support publishing statuses. For example, even after transitioning from a published workflow state to an unpublished workflow state they will still be visible to site visitors.", [
        '@entity_type_plural_label' => $entity_type
          ->getCollectionLabel(),
      ]);
    }
    return $features;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ModerationInformation::$bundleInfo protected property The bundle information service. Overrides ModerationInformation::$bundleInfo
ModerationInformation::$entityTypeManager protected property The entity type manager. Overrides ModerationInformation::$entityTypeManager
ModerationInformation::canModerateEntitiesOfEntityType public function Determines if an entity type can have moderated entities. Overrides ModerationInformation::canModerateEntitiesOfEntityType
ModerationInformation::getAffectedRevisionTranslation public function Returns the revision translation affected translation of a revision. Overrides ModerationInformation::getAffectedRevisionTranslation
ModerationInformation::getDefaultRevisionId public function Returns the revision ID of the default revision for the specified entity. Overrides ModerationInformation::getDefaultRevisionId
ModerationInformation::getLatestRevision public function Loads the latest revision of a specific entity. Overrides ModerationInformation::getLatestRevision
ModerationInformation::getLatestRevisionId public function Returns the revision ID of the latest revision of the given entity. Overrides ModerationInformation::getLatestRevisionId
ModerationInformation::getOriginalState public function Gets the original or initial state of the given entity. Overrides ModerationInformationInterface::getOriginalState
ModerationInformation::getUnsupportedFeatures public function Gets unsupported features for a given entity type. Overrides ModerationInformation::getUnsupportedFeatures
ModerationInformation::getWorkflowForEntity public function Gets the workflow for the given content entity. Overrides ModerationInformation::getWorkflowForEntity
ModerationInformation::getWorkflowForEntityTypeAndBundle public function Gets the workflow for the given entity type and bundle. Overrides ModerationInformationInterface::getWorkflowForEntityTypeAndBundle
ModerationInformation::hasPendingRevision public function Determines if a pending revision exists for the specified entity. Overrides ModerationInformation::hasPendingRevision
ModerationInformation::isDefaultRevisionPublished public function Determines if the default revision for the given entity is published. Overrides ModerationInformation::isDefaultRevisionPublished
ModerationInformation::isFirstTimeModeration protected function Determines if this entity is being moderated for the first time.
ModerationInformation::isLatestRevision public function Determines if an entity is a latest revision. Overrides ModerationInformation::isLatestRevision
ModerationInformation::isLiveRevision public function Determines if an entity is "live". Overrides ModerationInformation::isLiveRevision
ModerationInformation::isModeratedEntity public function Determines if an entity is moderated. Overrides ModerationInformation::isModeratedEntity
ModerationInformation::isModeratedEntityType public function Determines if an entity type has at least one moderated bundle. Overrides ModerationInformationInterface::isModeratedEntityType
ModerationInformation::shouldModerateConfigEntity public function Whether this type of config entity has moderation enabled.
ModerationInformation::shouldModerateEntitiesOfBundle public function Determines if an entity type/bundle entities should be moderated. Overrides ModerationInformation::shouldModerateEntitiesOfBundle
ModerationInformation::__construct public function Creates a new ModerationInformation instance. Overrides ModerationInformation::__construct
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.