You are here

public function ConfigEntityRevisionsOverviewFormBase::buildForm in Config Entity Revisions 1.x

Same name and namespace in other branches
  1. 8.2 src/ConfigEntityRevisionsOverviewFormBase.php \Drupal\config_entity_revisions\ConfigEntityRevisionsOverviewFormBase::buildForm()
  2. 8 src/ConfigEntityRevisionsOverviewFormBase.php \Drupal\config_entity_revisions\ConfigEntityRevisionsOverviewFormBase::buildForm()

Generates an overview table of older revisions of a config entity.

Parameters

array $form: A form being built.

FormStateInterface $form_state: The form state.

ConfigEntityInterface $configEntity: A configuration entity.

Return value

array An array as expected by \Drupal\Core\Render\RendererInterface::render().

Throws

\Drupal\Core\Entity\EntityMalformedException

Overrides FormInterface::buildForm

2 calls to ConfigEntityRevisionsOverviewFormBase::buildForm()
ViewsRevisionsOverviewForm::buildForm in modules/views_revisions/src/Form/ViewsRevisionsOverviewForm.php
Generates an overview table of older revisions of a config entity.
WebformRevisionsOverviewForm::buildForm in modules/webform_revisions/src/Form/WebformRevisionsOverviewForm.php
Generates an overview table of older revisions of a config entity.
2 methods override ConfigEntityRevisionsOverviewFormBase::buildForm()
ViewsRevisionsOverviewForm::buildForm in modules/views_revisions/src/Form/ViewsRevisionsOverviewForm.php
Generates an overview table of older revisions of a config entity.
WebformRevisionsOverviewForm::buildForm in modules/webform_revisions/src/Form/WebformRevisionsOverviewForm.php
Generates an overview table of older revisions of a config entity.

File

src/ConfigEntityRevisionsOverviewFormBase.php, line 174

Class

ConfigEntityRevisionsOverviewFormBase
Controller to make library functions available to various consumers.

Namespace

Drupal\config_entity_revisions

Code

public function buildForm(array $form, FormStateInterface $form_state, ConfigEntityInterface $configEntity = NULL) {

  // Disable for now, having seen https://www.drupal.org/project/diff/issues/2452523.
  $haveDiff = $this->moduleHandler
    ->moduleExists('diff') && FALSE;
  $configEntityType = $configEntity
    ->getEntityType()
    ->id();

  /* @var $contentEntityStorage \Drupal\Core\Entity\ContentEntityStorageInterface */
  $contentEntityStorage = $configEntity
    ->contentEntityStorage();
  $account = $this
    ->currentUser();

  /* @var $revisionsEntity \Drupal\config_entity_revisions\Entity\ConfigEntityRevisions */
  $revisionsEntity = $configEntity
    ->getContentEntity();
  if (!$revisionsEntity) {
    $form['#title'] = $this
      ->t('No revisions for %title', [
      '%title' => $configEntity
        ->label(),
    ]);
    $form['message'] = [
      '#theme' => 'markup',
      '#markup' => "No revisions have been created for this configuration entity yet.",
    ];
    return $form;
  }

  /** @var \Drupal\content_moderation\ModerationInformationInterface $moderation_info */
  $moderation_info = \Drupal::service('content_moderation.moderation_information');
  $moderation_enabled = $moderation_info
    ->shouldModerateEntitiesOfBundle($revisionsEntity
    ->getEntityType(), $revisionsEntity
    ->bundle());
  $revert_permission = ($account
    ->hasPermission("revert {$configEntityType} revisions") || $account
    ->hasPermission('revert all revisions') || $account
    ->hasPermission('administer nodes')) && $configEntity
    ->access('update');
  $delete_permission = ($account
    ->hasPermission("delete {$configEntityType} revisions") || $account
    ->hasPermission('delete all revisions') || $account
    ->hasPermission('administer nodes')) && $configEntity
    ->access('delete');
  $pagerLimit = 10;
  $query = $contentEntityStorage
    ->getQuery()
    ->condition($revisionsEntity
    ->getEntityType()
    ->getKey('id'), $revisionsEntity
    ->id())
    ->pager($pagerLimit)
    ->allRevisions()
    ->sort($revisionsEntity
    ->getEntityType()
    ->getKey('revision'), 'DESC')
    ->accessCheck(FALSE)
    ->execute();
  $vids = array_keys($query);
  $revision_count = count($vids);
  $table_header = [
    'revision' => $this
      ->t('Revision'),
  ];
  if ($configEntity
    ->has_own_content()) {
    $table_header['submissions'] = $this
      ->t('Submissions');
  }

  // Allow comparisons only if there are 2 or more revisions.
  if ($haveDiff && $revision_count > 1) {
    $table_header += [
      'select_column_one' => '',
      'select_column_two' => '',
    ];
  }
  if ($moderation_enabled) {
    $table_header['status'] = $this
      ->t('Status');
  }
  $table_header['operations'] = $this
    ->t('Operations');
  $form['revisions_table'] = [
    '#type' => 'table',
    '#header' => $table_header,
  ];
  if ($haveDiff) {
    $form['revisions_table'] += [
      '#attributes' => [
        'class' => [
          'diff-revisions',
        ],
      ],
      '#attached' => [
        'library' => [
          'diff/diff.general',
        ],
        'drupalSettings' => [
          'diffRevisionRadios' => $this->config
            ->get('general_settings.radio_behavior'),
        ],
      ],
    ];
  }
  $form['entity_type'] = [
    '#type' => 'hidden',
    '#value' => $configEntityType,
  ];
  $form['entity_id'] = [
    '#type' => 'hidden',
    '#value' => $configEntity
      ->id(),
  ];

  // We need to handle there being no published revisions, in which case
  // every row should have a 'Publish' button. In that situation, we can't
  // simply look at whether a revision is before or after the 'default'
  // revision.
  $published_revision_id = $revisionsEntity
    ->isPublished() ? $revisionsEntity
    ->getRevisionId() : 0;
  foreach ($vids as $vid) {

    /* @var $revision \Drupal\config_entity_revisions\Entity\ConfigEntityRevisions */
    $revision = $contentEntityStorage
      ->loadRevision($vid);
    $username = [
      '#theme' => 'username',
      '#account' => $revision
        ->getRevisionUser(),
    ];

    // Use revision link to link to revisions that are not active.
    $date = $this->dateFormatter
      ->format($revision
      ->getRevisionCreationTime(), 'short');
    if ($configEntity
      ->has_canonical_url() && $revision
      ->isDefaultRevision() && $revision
      ->isPublished()) {
      $link = $configEntity
        ->toLink($date)
        ->toString();
    }
    else {
      $url = new Url("entity.{$configEntityType}.revision", [
        $configEntityType => $configEntity
          ->id(),
        "revision_id" => $vid,
      ]);
      $link = $this->linkService
        ->generate($date, $url);
    }
    $column = [
      'data' => [
        '#type' => 'inline_template',
        '#template' => '{% trans %}{{ date }} by {{ username }}{% endtrans %}{% if message %}<p class="revision-log">{{ message }}</p>{% endif %}',
        '#context' => [
          'date' => $link,
          'username' => $this->renderer
            ->renderPlain($username),
          'message' => [
            '#markup' => $revision
              ->getRevisionLogMessage(),
            '#allowed_tags' => Xss::getHtmlTagList(),
          ],
        ],
      ],
    ];
    $this->renderer
      ->addCacheableDependency($column['data'], $username);
    $row = [
      'description' => $column,
    ];
    if ($configEntity
      ->has_own_content()) {
      $count = $configEntity
        ->contentEntityCount($vid);
      $pluralised = \Drupal::service('string_translation')
        ->formatPlural($count, '1 submission', '@count submissions');
      $row['submissions'] = [
        '#markup' => $count ? $pluralised : $this
          ->t('No submissions'),
      ];
    }

    // Allow comparisons only if there are 2 or more revisions.
    if ($haveDiff && $revision_count > 1) {
      $row += [
        'select_column_one' => $this
          ->buildSelectColumn('radios_left', $vid, FALSE),
        'select_column_two' => $this
          ->buildSelectColumn('radios_right', $vid, $vid),
      ];
    }
    if ($moderation_enabled) {
      $row['status'] = [
        '#markup' => $this
          ->t($revision->moderation_state->value),
      ];
    }
    if ($revision
      ->isDefaultRevision() && $revision
      ->isPublished()) {
      $row += [
        'data' => [
          '#prefix' => '<em>',
          '#markup' => $this
            ->t('Current revision'),
          '#suffix' => '</em>',
        ],
        '#attributes' => [
          'class' => [
            'revision-current',
          ],
        ],
      ];
    }
    else {
      $links = [];
      if ($revert_permission) {
        $links['revert'] = [
          'title' => $vid < $published_revision_id ? $this
            ->t('Revert') : $this
            ->t('Publish'),
          'url' => Url::fromRoute("entity.{$configEntityType}.revision_revert_confirm", [
            'config_entity' => $configEntity
              ->id(),
            "revision_id" => $vid,
          ]),
        ];
      }
      if ($delete_permission && $revision_count > 1) {
        $links['delete'] = [
          'title' => $this
            ->t('Delete'),
          'url' => Url::fromRoute("entity.{$configEntityType}.revision_delete_confirm", [
            'config_entity' => $configEntity
              ->id(),
            'revision_id' => $vid,
          ]),
        ];
      }
      $row['data'] = [
        '#type' => 'operations',
        '#links' => $links,
      ];
    }
    $form['revisions_table'][] = $row;
  }

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