You are here

public function EntityModeratedRevision::isTransitionedToUnpublished in Acquia Content Hub 8.2

Checks if the revision transitioning from published to unpublished state.

Parameters

\Drupal\Core\Entity\EntityInterface $entity: The entity revision being saved.

Return value

bool True if the original entity is published, but the current revision is unpublished.

File

modules/acquia_contenthub_publisher/src/EntityModeratedRevision.php, line 88

Class

EntityModeratedRevision
Determines whether an entity revision is "published".

Namespace

Drupal\acquia_contenthub_publisher

Code

public function isTransitionedToUnpublished(EntityInterface $entity) : bool {
  $status = $entity
    ->getEntityType()
    ->hasKey("status") ? $entity
    ->getEntityType()
    ->getKey("status") : NULL;
  if (!$status || !$entity instanceof RevisionableInterface) {

    // If the entity does not have a publishing status then
    // it is considered published.
    return TRUE;
  }
  $definition = $entity
    ->getFieldDefinition($status);
  $property = $definition
    ->getFieldStorageDefinition()
    ->getMainPropertyName();
  if (!$property) {
    $this->achPublisherChannel
      ->warning(sprintf('Cannot get status field main property name of entity. (%s, %s)', $entity
      ->getEntityTypeId(), $entity
      ->uuid()));
    return FALSE;
  }

  // If there is no original then the entity just created.
  if (!isset($entity->original)) {
    return FALSE;
  }

  // Handle multilingual entities here.
  $translation_origin_published = FALSE;
  if ($entity instanceof TranslatableInterface && $entity
    ->isTranslatable()) {

    // Loop through each language on the original
    // entity that has a translation.
    foreach ($entity->original
      ->getTranslationLanguages() as $language) {

      // Load the translated revision. If any of the origin
      // translation is published then we assume it was published.
      $translation = $entity->original
        ->getTranslation($language
        ->getId());
      if ($translation
        ->get($status)->{$property}) {
        $translation_origin_published = TRUE;
        break;
      }
    }
  }
  $original_is_published = (bool) $entity->original
    ->get($status)->{$property} || $translation_origin_published;

  // If current status is unpublished and the original is published,
  // then we should export.
  return (bool) $entity
    ->get($status)->{$property} === FALSE && $original_is_published;
}