You are here

public function ModerationSidebarController::revisionOverview in Moderation Sidebar 8

Generates an simple list of revisions for a node.

Parameters

\Drupal\node\NodeInterface $node: A node object.

Return value

array An array as expected by drupal_render().

1 string reference to 'ModerationSidebarController::revisionOverview'
moderation_sidebar.routing.yml in ./moderation_sidebar.routing.yml
moderation_sidebar.routing.yml

File

src/Controller/ModerationSidebarController.php, line 334

Class

ModerationSidebarController
Endpoints for the Moderation Sidebar module.

Namespace

Drupal\moderation_sidebar\Controller

Code

public function revisionOverview(NodeInterface $node) {
  $langcode = $node
    ->language()
    ->getId();
  $node_storage = $this
    ->entityTypeManager()
    ->getStorage('node');
  $result = $node_storage
    ->getQuery()
    ->allRevisions()
    ->condition($node
    ->getEntityType()
    ->getKey('id'), $node
    ->id())
    ->sort($node
    ->getEntityType()
    ->getKey('revision'), 'DESC')
    ->execute();
  $build = $this
    ->getBackButton($node);
  $count = 0;
  foreach (array_keys($result) as $vid) {
    if ($count >= 5) {
      break;
    }

    /** @var \Drupal\node\NodeInterface $revision */
    $revision = $node_storage
      ->loadRevision($vid);

    // Only show revisions that are affected by the language that is being
    // displayed.
    if ($revision
      ->hasTranslation($langcode) && $revision
      ->getTranslation($langcode)
      ->isRevisionTranslationAffected()) {
      $user = $revision
        ->getRevisionUser();
      $message = !empty($revision->revision_log->value) ? $revision->revision_log->value : $this
        ->t('No message');

      // Use revision link to link to revisions that are not active.
      $time = $revision->revision_timestamp->value;
      $pretty_time = $this
        ->getPrettyTime($revision->revision_timestamp->value);
      if ($vid != $node
        ->getRevisionId()) {
        $link = new Link($pretty_time, new Url('entity.node.revision', [
          'node' => $node
            ->id(),
          'node_revision' => $vid,
        ]));
      }
      else {
        $link = $node
          ->toLink($pretty_time);
      }
      $build[] = [
        '#theme' => 'moderation_sidebar_revision',
        '#revision_message' => [
          '#markup' => $message,
          '#allowed_tags' => Xss::getHtmlTagList(),
        ],
        '#revision_time' => $time,
        '#revision_time_pretty' => $pretty_time,
        '#revision_author' => $user
          ->getDisplayName(),
        '#revision_author_link' => $user
          ->toLink()
          ->toRenderable(),
        '#revision_link' => $link,
      ];
      ++$count;
    }
  }
  $build[] = [
    '#title' => $this
      ->t('View all revisions'),
    '#type' => 'link',
    '#url' => Url::fromRoute('entity.node.version_history', [
      'node' => $node
        ->id(),
    ]),
    '#attributes' => [
      'class' => [
        'moderation-sidebar-link',
        'button',
      ],
    ],
  ];
  return $build;
}