You are here

public function RevisionOverviewForm::buildForm in Diff 8

Form constructor.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array The form structure.

Overrides FormInterface::buildForm

File

src/Form/RevisionOverviewForm.php, line 135

Class

RevisionOverviewForm
Provides a form for revision overview page.

Namespace

Drupal\diff\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, $node = NULL) {
  $account = $this->currentUser;

  /** @var \Drupal\node\NodeInterface $node */
  $langcode = $node
    ->language()
    ->getId();
  $langname = $node
    ->language()
    ->getName();
  $languages = $node
    ->getTranslationLanguages();
  $has_translations = count($languages) > 1;
  $node_storage = $this->entityTypeManager
    ->getStorage('node');
  $type = $node
    ->getType();
  $pagerLimit = $this->config
    ->get('general_settings.revision_pager_limit');
  $query = $this->entityTypeManager
    ->getStorage('node')
    ->getQuery()
    ->condition($node
    ->getEntityType()
    ->getKey('id'), $node
    ->id())
    ->pager($pagerLimit)
    ->allRevisions()
    ->sort($node
    ->getEntityType()
    ->getKey('revision'), 'DESC')
    ->accessCheck(FALSE)
    ->execute();
  $vids = array_keys($query);
  $revision_count = count($vids);
  $build['#title'] = $has_translations ? $this
    ->t('@langname revisions for %title', [
    '@langname' => $langname,
    '%title' => $node
      ->label(),
  ]) : $this
    ->t('Revisions for %title', [
    '%title' => $node
      ->label(),
  ]);
  $build['nid'] = array(
    '#type' => 'hidden',
    '#value' => $node
      ->id(),
  );
  $table_header = [];
  $table_header['revision'] = $this
    ->t('Revision');

  // Allow comparisons only if there are 2 or more revisions.
  if ($revision_count > 1) {
    $table_header += array(
      'select_column_one' => '',
      'select_column_two' => '',
    );
  }
  $table_header['operations'] = $this
    ->t('Operations');
  $rev_revert_perm = $account
    ->hasPermission("revert {$type} revisions") || $account
    ->hasPermission('revert all revisions') || $account
    ->hasPermission('administer nodes');
  $rev_delete_perm = $account
    ->hasPermission("delete {$type} revisions") || $account
    ->hasPermission('delete all revisions') || $account
    ->hasPermission('administer nodes');
  $revert_permission = $rev_revert_perm && $node
    ->access('update');
  $delete_permission = $rev_delete_perm && $node
    ->access('delete');

  // Contains the table listing the revisions.
  $build['node_revisions_table'] = array(
    '#type' => 'table',
    '#header' => $table_header,
    '#attributes' => array(
      'class' => array(
        'diff-revisions',
      ),
    ),
  );
  $build['node_revisions_table']['#attached']['library'][] = 'diff/diff.general';
  $build['node_revisions_table']['#attached']['drupalSettings']['diffRevisionRadios'] = $this->config
    ->get('general_settings.radio_behavior');
  $default_revision = $node
    ->getRevisionId();

  // Add rows to the table.
  foreach ($vids as $key => $vid) {
    $previous_revision = NULL;
    if (isset($vids[$key + 1])) {
      $previous_revision = $node_storage
        ->loadRevision($vids[$key + 1]);
    }

    /** @var \Drupal\Core\Entity\ContentEntityInterface $revision */
    if ($revision = $node_storage
      ->loadRevision($vid)) {
      if ($revision
        ->hasTranslation($langcode) && $revision
        ->getTranslation($langcode)
        ->isRevisionTranslationAffected()) {
        $username = array(
          '#theme' => 'username',
          '#account' => $revision
            ->getRevisionUser(),
        );
        $revision_date = $this->date
          ->format($revision
          ->getRevisionCreationTime(), 'short');

        // Use revision link to link to revisions that are not active.
        if ($vid != $node
          ->getRevisionId()) {
          $link = Link::fromTextAndUrl($revision_date, new Url('entity.node.revision', [
            'node' => $node
              ->id(),
            'node_revision' => $vid,
          ]));
        }
        else {
          $link = $node
            ->toLink($revision_date);
        }
        if ($vid == $default_revision) {
          $row = [
            'revision' => $this
              ->buildRevision($link, $username, $revision, $previous_revision),
          ];

          // Allow comparisons only if there are 2 or more revisions.
          if ($revision_count > 1) {
            $row += [
              'select_column_one' => $this
                ->buildSelectColumn('radios_left', $vid, FALSE),
              'select_column_two' => $this
                ->buildSelectColumn('radios_right', $vid, $vid),
            ];
          }
          $row['operations'] = array(
            '#prefix' => '<em>',
            '#markup' => $this
              ->t('Current revision'),
            '#suffix' => '</em>',
            '#attributes' => array(
              'class' => array(
                'revision-current',
              ),
            ),
          );
          $row['#attributes'] = [
            'class' => [
              'revision-current',
            ],
          ];
        }
        else {
          $route_params = array(
            'node' => $node
              ->id(),
            'node_revision' => $vid,
            'langcode' => $langcode,
          );
          $links = array();
          if ($revert_permission) {
            $links['revert'] = [
              'title' => $vid < $node
                ->getRevisionId() ? $this
                ->t('Revert') : $this
                ->t('Set as current revision'),
              'url' => $has_translations ? Url::fromRoute('node.revision_revert_translation_confirm', [
                'node' => $node
                  ->id(),
                'node_revision' => $vid,
                'langcode' => $langcode,
              ]) : Url::fromRoute('node.revision_revert_confirm', [
                'node' => $node
                  ->id(),
                'node_revision' => $vid,
              ]),
            ];
          }
          if ($delete_permission) {
            $links['delete'] = array(
              'title' => $this
                ->t('Delete'),
              'url' => Url::fromRoute('node.revision_delete_confirm', $route_params),
            );
          }

          // Here we don't have to deal with 'only one revision' case because
          // if there's only one revision it will also be the default one,
          // entering on the first branch of this if else statement.
          $row = [
            'revision' => $this
              ->buildRevision($link, $username, $revision, $previous_revision),
            'select_column_one' => $this
              ->buildSelectColumn('radios_left', $vid, isset($vids[1]) ? $vids[1] : FALSE),
            'select_column_two' => $this
              ->buildSelectColumn('radios_right', $vid, FALSE),
            'operations' => [
              '#type' => 'operations',
              '#links' => $links,
            ],
          ];
        }

        // Add the row to the table.
        $build['node_revisions_table'][] = $row;
      }
    }
  }

  // Allow comparisons only if there are 2 or more revisions.
  if ($revision_count > 1) {
    $build['submit'] = array(
      '#type' => 'submit',
      '#button_type' => 'primary',
      '#value' => t('Compare selected revisions'),
      '#attributes' => array(
        'class' => array(
          'diff-button',
        ),
      ),
    );
  }
  $build['pager'] = array(
    '#type' => 'pager',
  );
  $build['#attached']['library'][] = 'node/drupal.node.admin';
  return $build;
}