You are here

class ModerationInformation in Workbench Moderation 8

Same name and namespace in other branches
  1. 8.2 src/ModerationInformation.php \Drupal\workbench_moderation\ModerationInformation

General service for moderation-related questions about Entity API.

@todo Much of this code may eventually migrate to the Entity module, and from there to Drupal core.

Hierarchy

Expanded class hierarchy of ModerationInformation

4 files declare their use of ModerationInformation
EditTab.php in src/Plugin/Menu/EditTab.php
LatestRevisionCheckTest.php in tests/src/Unit/LatestRevisionCheckTest.php
ModerationInformationTest.php in tests/src/Unit/ModerationInformationTest.php
ModerationStateWidget.php in src/Plugin/Field/FieldWidget/ModerationStateWidget.php
1 string reference to 'ModerationInformation'
workbench_moderation.services.yml in ./workbench_moderation.services.yml
workbench_moderation.services.yml
1 service uses ModerationInformation
workbench_moderation.moderation_information in ./workbench_moderation.services.yml
Drupal\workbench_moderation\ModerationInformation

File

src/ModerationInformation.php, line 22

Namespace

Drupal\workbench_moderation
View source
class ModerationInformation implements ModerationInformationInterface {

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

  /**
   * The current user.
   *
   * @var \Drupal\Core\Session\AccountInterface
   */
  protected $currentUser;

  /**
   * Creates a new ModerationInformation instance.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   * @param \Drupal\Core\Session\AccountInterface $current_user
   *   The current user.
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager, AccountInterface $current_user) {
    $this->entityTypeManager = $entity_type_manager;
    $this->currentUser = $current_user;
  }

  /**
   * {@inheritdoc}
   */
  public function isModeratableEntity(EntityInterface $entity) {
    if (!$entity instanceof ContentEntityInterface) {
      return FALSE;
    }
    return $this
      ->isModeratableBundle($entity
      ->getEntityType(), $entity
      ->bundle());
  }

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

  /**
   * {@inheritdoc}
   */
  public function loadBundleEntity($bundle_entity_type_id, $bundle_id) {
    if ($bundle_entity_type_id) {
      return $this->entityTypeManager
        ->getStorage($bundle_entity_type_id)
        ->load($bundle_id);
    }
  }

  /**
   * {@inheritdoc}
   */
  public function isModeratableBundle(EntityTypeInterface $entity_type, $bundle) {
    if ($bundle_entity = $this
      ->loadBundleEntity($entity_type
      ->getBundleEntityType(), $bundle)) {
      return $bundle_entity
        ->getThirdPartySetting('workbench_moderation', 'enabled', FALSE);
    }
    return FALSE;
  }

  /**
   * {@inheritdoc}
   */
  public function selectRevisionableEntityTypes(array $entity_types) {
    return array_filter($entity_types, function (EntityTypeInterface $type) use ($entity_types) {
      return $type instanceof ConfigEntityTypeInterface && ($bundle_of = $type
        ->get('bundle_of')) && $entity_types[$bundle_of]
        ->isRevisionable();
    });
  }

  /**
   * {@inheritdoc}
   */
  public function selectRevisionableEntities(array $entity_types) {
    return array_filter($entity_types, function (EntityTypeInterface $type) use ($entity_types) {
      return $type instanceof ContentEntityTypeInterface && $type
        ->isRevisionable() && $type
        ->getBundleEntityType();
    });
  }

  /**
   * {@inheritdoc}
   */
  public function isBundleForModeratableEntity(EntityInterface $entity) {
    $type = $entity
      ->getEntityType();
    return $type instanceof ConfigEntityTypeInterface && ($bundle_of = $type
      ->get('bundle_of')) && $this->entityTypeManager
      ->getDefinition($bundle_of)
      ->isRevisionable() && $this->currentUser
      ->hasPermission('administer moderation states');
  }

  /**
   * {@inheritdoc}
   */
  public function isModeratedEntityForm(FormInterface $form_object) {
    return $form_object instanceof ContentEntityFormInterface && $this
      ->isModeratableEntity($form_object
      ->getEntity());
  }

  /**
   * {@inheritdoc}
   */
  public function isRevisionableBundleForm(FormInterface $form_object) {

    // We really shouldn't be checking for a base class, but core lacks an
    // interface here. When core adds a better way to determine if we're on
    // a Bundle configuration form we should switch to that.
    if ($form_object instanceof BundleEntityFormBase) {
      $bundle_of = $form_object
        ->getEntity()
        ->getEntityType()
        ->getBundleOf();
      $type = $this->entityTypeManager
        ->getDefinition($bundle_of);
      return $type
        ->isRevisionable();
    }
    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)) {
      $revision_ids = $storage
        ->getQuery()
        ->allRevisions()
        ->condition($this->entityTypeManager
        ->getDefinition($entity_type_id)
        ->getKey('id'), $entity_id)
        ->sort($this->entityTypeManager
        ->getDefinition($entity_type_id)
        ->getKey('revision'), 'DESC')
        ->range(0, 1)
        ->execute();
      if ($revision_ids) {
        $revision_id = array_keys($revision_ids)[0];
        return $revision_id;
      }
    }
  }

  /**
   * {@inheritdoc}
   */
  public function getDefaultRevisionId($entity_type_id, $entity_id) {
    if ($storage = $this->entityTypeManager
      ->getStorage($entity_type_id)) {
      $revision_ids = $storage
        ->getQuery()
        ->condition($this->entityTypeManager
        ->getDefinition($entity_type_id)
        ->getKey('id'), $entity_id)
        ->sort($this->entityTypeManager
        ->getDefinition($entity_type_id)
        ->getKey('revision'), 'DESC')
        ->range(0, 1)
        ->execute();
      if ($revision_ids) {
        $revision_id = array_keys($revision_ids)[0];
        return $revision_id;
      }
    }
  }

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

  /**
   * {@inheritdoc}
   */
  public function hasForwardRevision(ContentEntityInterface $entity) {
    return $this
      ->isModeratableEntity($entity) && !($this
      ->getLatestRevisionId($entity
      ->getEntityTypeId(), $entity
      ->id()) == $this
      ->getDefaultRevisionId($entity
      ->getEntityTypeId(), $entity
      ->id()));
  }

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

}

Members

Namesort descending Modifiers Type Description Overrides
ModerationInformation::$currentUser protected property The current user.
ModerationInformation::$entityTypeManager protected property The entity type manager.
ModerationInformation::getDefaultRevisionId public function Returns the revision ID of the default revision for the specified entity. Overrides ModerationInformationInterface::getDefaultRevisionId
ModerationInformation::getLatestRevision public function Loads the latest revision of a specific entity. Overrides ModerationInformationInterface::getLatestRevision
ModerationInformation::getLatestRevisionId public function Returns the revision ID of the latest revision of the given entity. Overrides ModerationInformationInterface::getLatestRevisionId
ModerationInformation::hasForwardRevision public function Determines if a forward revision exists for the specified entity. Overrides ModerationInformationInterface::hasForwardRevision
ModerationInformation::isBundleForModeratableEntity public function Determines if config entity is a bundle for entities that may be moderated. Overrides ModerationInformationInterface::isBundleForModeratableEntity
ModerationInformation::isLatestRevision public function Determines if an entity is a latest revision. Overrides ModerationInformationInterface::isLatestRevision
ModerationInformation::isLiveRevision public function Determines if an entity is "live". Overrides ModerationInformationInterface::isLiveRevision
ModerationInformation::isModeratableBundle public function Determines if an entity type/bundle is one that will be moderated. Overrides ModerationInformationInterface::isModeratableBundle
ModerationInformation::isModeratableEntity public function Determines if an entity is one we should be moderating. Overrides ModerationInformationInterface::isModeratableEntity
ModerationInformation::isModeratableEntityType public function Determines if an entity type has been marked as moderatable. Overrides ModerationInformationInterface::isModeratableEntityType
ModerationInformation::isModeratedEntityForm public function Determines if this form is for a moderated entity. Overrides ModerationInformationInterface::isModeratedEntityForm
ModerationInformation::isRevisionableBundleForm public function Determines if the form is the bundle edit of a revisionable entity. Overrides ModerationInformationInterface::isRevisionableBundleForm
ModerationInformation::loadBundleEntity public function Loads a specific bundle entity. Overrides ModerationInformationInterface::loadBundleEntity
ModerationInformation::selectRevisionableEntities public function Filters an entity list to just the definitions for moderatable entities. Overrides ModerationInformationInterface::selectRevisionableEntities
ModerationInformation::selectRevisionableEntityTypes public function Filters an entity list to bundle definitions for revisionable entities. Overrides ModerationInformationInterface::selectRevisionableEntityTypes
ModerationInformation::__construct public function Creates a new ModerationInformation instance.