You are here

public function ModerationInformation::isDefaultRevisionPublished in Config Entity Revisions 8.2

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 ModerationInformation::isDefaultRevisionPublished

File

src/ModerationInformation.php, line 195

Class

ModerationInformation
Class ModerationInformation.

Namespace

Drupal\config_entity_revisions

Code

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();
}