You are here

protected function ScheduledTransitionAddForm::getRevisionOptions in Scheduled Transitions 8

Same name and namespace in other branches
  1. 2.x src/Form/Entity/ScheduledTransitionAddForm.php \Drupal\scheduled_transitions\Form\Entity\ScheduledTransitionAddForm::getRevisionOptions()

Get revisions for an entity as options for a tableselect.

Parameters

\Drupal\Core\Entity\EntityInterface $entity: Get revisions for this entity.

Return value

array An array of options suitable for a tableselect element.

1 call to ScheduledTransitionAddForm::getRevisionOptions()
ScheduledTransitionAddForm::form in src/Form/Entity/ScheduledTransitionAddForm.php
Gets the actual form array to be built.

File

src/Form/Entity/ScheduledTransitionAddForm.php, line 373

Class

ScheduledTransitionAddForm
Scheduled transitions add form.

Namespace

Drupal\scheduled_transitions\Form\Entity

Code

protected function getRevisionOptions(EntityInterface $entity) : array {
  $entityTypeId = $entity
    ->getEntityTypeId();
  $entityDefinition = $this->entityTypeManager
    ->getDefinition($entityTypeId);
  $entityStorage = $this->entityTypeManager
    ->getStorage($entityTypeId);
  $workflow = $this->moderationInformation
    ->getWorkflowForEntity($entity);
  $workflowPlugin = $workflow
    ->getTypePlugin();
  $workflowStates = $workflowPlugin ? $workflowPlugin
    ->getStates() : [];

  /** @var int[] $ids */
  $ids = $entityStorage
    ->getQuery()
    ->allRevisions()
    ->condition($entityDefinition
    ->getKey('id'), $entity
    ->id())
    ->condition($entityDefinition
    ->getKey('langcode'), $this->languageManager
    ->getCurrentLanguage(LanguageInterface::TYPE_CONTENT)
    ->getId())
    ->sort($entityDefinition
    ->getKey('revision'), 'DESC')
    ->execute();
  $revisionIds = array_keys($ids);
  $entityRevisions = array_map(function (string $revisionId) use ($entityStorage) : EntityInterface {
    $revision = $entityStorage
      ->loadRevision($revisionId);

    // When the entity is translatable, load the translation for the current
    // language.
    if ($revision instanceof TranslatableInterface) {
      $revision = $revision
        ->getTranslation($this->languageManager
        ->getCurrentLanguage(LanguageInterface::TYPE_CONTENT)
        ->getId());
    }
    return $revision;
  }, array_combine($revisionIds, $revisionIds));

  // When the entity is translatable, every revision contains a copy for every
  // translation. We only want to show the revisions that affected the
  // translation for the current language.
  $entityRevisions = array_filter($entityRevisions, function (EntityInterface $revision) {
    return $revision instanceof TranslatableRevisionableInterface ? $revision
      ->isRevisionTranslationAffected() : TRUE;
  });
  $options = array_map(function (EntityInterface $entityRevision) use ($workflowStates) : array {

    /** @var \Drupal\Core\Entity\EntityInterface|\Drupal\Core\Entity\RevisionableInterface $entityRevision */
    $option = [];
    $revisionTArgs = [
      '@revision_id' => $entityRevision
        ->getRevisionId(),
    ];

    // Dont add the arg to toLink in case this particular entity has
    // overwritten the default value of the param.
    $toLinkArgs = [
      $this
        ->t('#@revision_id', $revisionTArgs),
    ];
    if ($entityRevision
      ->hasLinkTemplate('revision')) {
      $toLinkArgs[] = 'revision';
    }
    $revisionLink = $entityRevision
      ->toLink(...$toLinkArgs);
    $revisionCell = $revisionLink
      ->toRenderable();
    $revisionCell['#attributes'] = [
      'target' => '_blank',
    ];
    $option['revision_id']['data'] = $revisionCell;
    $moderationState = $workflowStates[$entityRevision->moderation_state->value] ?? NULL;
    $option['state']['data'] = $moderationState ? $moderationState
      ->label() : $this
      ->t('- Unknown state -');
    if ($entityRevision instanceof RevisionLogInterface) {
      $option['revision_time']['data']['#plain_text'] = $this->dateFormatter
        ->format($entityRevision
        ->getRevisionCreationTime());
      $revisionUser = $entityRevision
        ->getRevisionUser();
      if ($revisionUser) {
        $option['revision_author']['data'] = $this->moduleHandler
          ->moduleExists('user') ? [
          '#theme' => 'username',
          '#account' => $revisionUser,
        ] : $revisionUser
          ->toLink();
      }
      else {
        $option['revision_author']['data'] = $this
          ->t('- Missing user -');
      }
      if ($revisionLog = $entityRevision
        ->getRevisionLogMessage()) {
        $option['revision_log']['data'] = [
          '#markup' => $revisionLog,
          '#allowed_tags' => Xss::getHtmlTagList(),
        ];
      }
      else {
        $option['revision_log']['data'] = $this
          ->t('<em>- None -</em>');
      }
    }
    return $option;
  }, $entityRevisions);
  $options = [
    static::LATEST_REVISION => [
      'revision_id' => [
        'data' => $this
          ->t('Latest revision'),
      ],
      'state' => [
        'data' => $this
          ->t('Automatically determines the latest revision at time of transition.'),
        'colspan' => $entity instanceof RevisionLogInterface ? 4 : 1,
      ],
    ],
  ] + $options;
  return $options;
}