You are here

public function ScheduledTransitionForm::form in Scheduled Transitions 8

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

Gets the actual form array to be built.

Overrides ContentEntityForm::form

See also

\Drupal\Core\Entity\EntityForm::processForm()

\Drupal\Core\Entity\EntityForm::afterBuild()

File

src/Form/ScheduledTransitionForm.php, line 70

Class

ScheduledTransitionForm
Show all scheduled transitions for an entity.

Namespace

Drupal\scheduled_transitions\Form

Code

public function form(array $form, FormStateInterface $form_state) : array {
  $entity = $this
    ->getEntity();
  $tableHeadings = [
    'from_revision' => [
      'data' => $this
        ->t('From revision'),
    ],
    'from_state' => [
      'data' => $this
        ->t('From state'),
    ],
    'to_state' => [
      'data' => $this
        ->t('To state'),
    ],
    'date' => [
      'data' => $this
        ->t('On date'),
      'field' => 'transition_on',
      'specifier' => 'transition_on',
      'sort' => 'asc',
    ],
    'author' => [
      'data' => $this
        ->t('Scheduled by'),
    ],
    'operations' => [
      'data' => $this
        ->t('Operations'),
    ],
  ];
  $form['table'] = [
    '#type' => 'table',
    '#header' => $tableHeadings,
    '#empty' => $this
      ->t('There are no scheduled transitions for @entity', [
      '@entity' => $entity
        ->label(),
    ]),
  ];
  $entityTypeId = $entity
    ->getEntityTypeId();
  $entityStorage = $this->entityTypeManager
    ->getStorage($entityTypeId);
  $transitionStorage = $this->entityTypeManager
    ->getStorage('scheduled_transition');
  $ids = $transitionStorage
    ->getQuery()
    ->condition('entity__target_type', $entityTypeId)
    ->condition('entity__target_id', $entity
    ->id())
    ->condition('entity_revision_langcode', $this->languageManager
    ->getCurrentLanguage(LanguageInterface::TYPE_CONTENT)
    ->getId())
    ->tableSort($tableHeadings)
    ->execute();
  $form['table']['#rows'] = array_map(function (ScheduledTransitionInterface $scheduledTransition) use ($entityStorage) {
    $row = [];
    $workflowPlugin = $scheduledTransition
      ->getWorkflow()
      ->getTypePlugin();
    $workflowStates = $workflowPlugin ? $workflowPlugin
      ->getStates() : [];

    // From...
    $entityRevisionId = $scheduledTransition
      ->getEntityRevisionId();
    $entityRevision = $entityStorage
      ->loadRevision($entityRevisionId);
    $revisionTArgs = [
      '@revision_id' => $entityRevisionId,
    ];
    if ($entityRevision) {
      $toLinkArgs = [
        $this
          ->t('#@revision_id', $revisionTArgs),
      ];
      if ($entityRevision
        ->hasLinkTemplate('revision')) {
        $toLinkArgs[] = 'revision';
      }
      $row['from_revision'] = $entityRevision
        ->toLink(...$toLinkArgs);
      $fromState = $workflowStates[$entityRevision->moderation_state->value] ?? NULL;
      $row['from_state'] = $fromState ? $fromState
        ->label() : $this
        ->t('- Missing from workflow/state -');
    }
    else {
      if (is_numeric($entityRevisionId) && $entityRevisionId > 0) {
        $row['from_revision'] = [
          // Span 'from_revision', 'from_state'.
          'colspan' => 2,
          'data' => $this
            ->t('Deleted revision #@revision_id', $revisionTArgs),
        ];
      }
      else {
        $options = $scheduledTransition
          ->getOptions();
        $text = isset($options[ScheduledTransition::OPTION_LATEST_REVISION]) ? $this
          ->t('Latest revision') : $this
          ->t('Dynamic');
        $row['from_revision'] = [
          'colspan' => 2,
          'data' => $text,
        ];
      }
    }

    // To.
    $toState = $workflowStates[$scheduledTransition
      ->getState()] ?? NULL;
    $row['to_state'] = $toState ? $toState
      ->label() : $this
      ->t('- Missing to workflow/state -');

    // Date.
    $time = $scheduledTransition
      ->getTransitionTime();
    $row['date'] = $this->dateFormatter
      ->format($time);

    // Author.
    $author = $scheduledTransition
      ->getAuthor();
    if ($author) {
      $row['author']['data'] = $this->moduleHandler
        ->moduleExists('user') ? [
        '#theme' => 'username',
        '#account' => $author,
      ] : $author
        ->toLink();
    }
    else {
      $row['author']['data'] = $this
        ->t('- Missing user -');
    }

    // Operations.
    $operations = $this->entityTypeManager
      ->getListBuilder('scheduled_transition')
      ->getOperations($scheduledTransition);
    $row['operations']['data'] = [
      '#type' => 'operations',
      '#links' => $operations,
    ];
    return $row;
  }, $transitionStorage
    ->loadMultiple($ids));
  return $form;
}