You are here

function _revision_log_default_get_original in Revision Log Default 8

Gets the original entity for an entity about to be saved.

Parameters

\Drupal\Core\Entity\ContentEntityInterface $entity: The entity that is about to be saved.

Return value

\Drupal\Core\Entity\ContentEntityInterface The original entity.

1 call to _revision_log_default_get_original()
revision_log_default_entity_presave in ./revision_log_default.module
Implements hook_entity_presave().

File

./revision_log_default.module, line 124
Contains hook implementations for the revision_log_default module.

Code

function _revision_log_default_get_original(ContentEntityInterface $entity) {

  // If the entity uses Moderation, load the latest revision as the original.
  // This could be brought into core, but do users expect ->original to be the
  // default revision or the last revision? It's complicated!
  $handler = \Drupal::moduleHandler();
  $is_moderated = FALSE;
  if ($handler
    ->moduleExists('content_moderation')) {

    /** @var \Drupal\content_moderation\ModerationInformation $information */
    $information = \Drupal::service('content_moderation.moderation_information');
    $is_moderated = $information
      ->isModeratedEntity($entity);
  }
  elseif ($handler
    ->moduleExists('workbench_moderation')) {

    /** @var \Drupal\workbench_moderation\ModerationInformation $information */
    $information = \Drupal::service('workbench_moderation.moderation_information');
    $is_moderated = $information
      ->isModeratableEntity($entity);
  }
  if ($is_moderated && $entity
    ->id()) {
    $node_storage = \Drupal::entityTypeManager()
      ->getStorage('node');
    $latest_revision_id = $node_storage
      ->getLatestRevisionId($entity
      ->id());
    $latest = $node_storage
      ->loadRevision($latest_revision_id);
    $langcode = $entity
      ->language()
      ->getId();
    if ($latest && $latest
      ->hasTranslation($langcode)) {
      return $latest
        ->getTranslation($langcode);
    }
  }
  return $entity->original;
}