You are here

public function UserController::revisionOverview in User Revision 8

Generates an overview table of older revisions of a user.

Parameters

\Drupal\user\UserInterface $user: A user object.

Return value

array An array as expected by drupal_render().

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

\Drupal\Component\Plugin\Exception\PluginNotFoundException

\Drupal\Core\Entity\EntityMalformedException

1 string reference to 'UserController::revisionOverview'
user_revision.routing.yml in ./user_revision.routing.yml
user_revision.routing.yml

File

src/Controller/UserController.php, line 71

Class

UserController
Returns responses for User revision routes.

Namespace

Drupal\user_revision\Controller

Code

public function revisionOverview(UserInterface $user) {
  $account = $this
    ->currentUser();
  $user_storage = $this->entityTypeManager
    ->getStorage('user');
  $build = array();
  $build['#title'] = $this
    ->t('Revisions for %title', array(
    '%title' => $user
      ->label(),
  ));
  $header = array(
    $this
      ->t('Revision'),
    $this
      ->t('Operations'),
  );
  $rows = array();
  $vids = user_revision_ids($user);
  foreach (array_reverse($vids) as $vid) {
    if ($revision = $user_storage
      ->loadRevision($vid)) {
      $revision_author = $revision->revision_uid->entity;
      $username = [
        '#theme' => 'username',
        '#account' => $revision_author,
      ];

      // Use revision link to link to revisions that are not active.
      $date = $this->dateFormatter
        ->format($revision->revision_timestamp->value, 'short');
      if ($vid == $user
        ->getRevisionId()) {
        $link = $user
          ->toLink($date)
          ->toString();
      }
      else {
        $link = Link::fromTextAndUrl($date, new Url('entity.user.revision', array(
          'user' => $user
            ->id(),
          'user_revision' => $vid,
        )))
          ->toString();
      }
      $row = [];
      $column = [
        'data' => [
          '#type' => 'inline_template',
          '#template' => '{% trans %}{{ date }} by {{ username }}{% endtrans %}{% if message %}<p class="revision-log">{{ message }}</p>{% endif %}',
          '#context' => [
            'date' => $link,
            'username' => \Drupal::service('renderer')
              ->render($username),
            'message' => [
              '#markup' => $revision->revision_log->value,
              '#allowed_tags' => Xss::getHtmlTagList(),
            ],
          ],
        ],
      ];

      // @todo Simplify once https://www.drupal.org/node/2334319 lands.
      // $this->renderer->addCacheableDependency($column['data'], $username);
      $row[] = $column;
      if ($vid == $user
        ->getRevisionId()) {
        $row[] = [
          'data' => [
            '#prefix' => '<em>',
            '#markup' => $this
              ->t('Current revision'),
            '#suffix' => '</em>',
          ],
        ];
        $rows[] = [
          'data' => $row,
          'class' => [
            'revision-current',
          ],
        ];
      }
      else {
        $links = [];
        if ($this->userRevisionAccessCheck
          ->checkAccess($revision, $account, 'update')) {
          $links['revert'] = [
            'title' => $vid < $user
              ->getRevisionId() ? $this
              ->t('Revert') : $this
              ->t('Set as current revision'),
            'url' => Url::fromRoute('user.revision_revert_confirm', [
              'user' => $user
                ->id(),
              'user_revision' => $vid,
            ]),
          ];
        }
        if ($this->userRevisionAccessCheck
          ->checkAccess($revision, $account, 'delete')) {
          $links['delete'] = [
            'title' => $this
              ->t('Delete'),
            'url' => Url::fromRoute('user.revision_delete_confirm', [
              'user' => $user
                ->id(),
              'user_revision' => $vid,
            ]),
          ];
        }
        $row[] = [
          'data' => [
            '#type' => 'operations',
            '#links' => $links,
          ],
        ];
        $rows[] = $row;
      }
    }
  }
  $build['user_revisions_table'] = array(
    '#theme' => 'table',
    '#rows' => $rows,
    '#header' => $header,
    '#attached' => array(
      'library' => array(
        'user_revision/user.admin',
      ),
    ),
  );
  return $build;
}