You are here

public function ModerationInformation::isDefaultRevisionPublished in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/content_moderation/src/ModerationInformation.php \Drupal\content_moderation\ModerationInformation::isDefaultRevisionPublished()

Determines if the default revision for the given entity is published.

The default revision is the same as the entity retrieved by "default" from the storage handler. If the entity is translated, check if any of the translations are published.

Parameters

\Drupal\Core\Entity\ContentEntityInterface $entity: The entity being saved.

Return value

bool TRUE if the default revision is published. FALSE otherwise.

Overrides ModerationInformationInterface::isDefaultRevisionPublished

File

core/modules/content_moderation/src/ModerationInformation.php, line 151

Class

ModerationInformation
General service for moderation-related questions about Entity API.

Namespace

Drupal\content_moderation

Code

public function isDefaultRevisionPublished(ContentEntityInterface $entity) {
  $workflow = $this
    ->getWorkflowForEntity($entity);
  $default_revision = $this->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();
}