You are here

public function MediaRevisionController::revisionOverview in Media Revisions UI 2.0.x

Same name and namespace in other branches
  1. 8 src/Controller/MediaRevisionController.php \Drupal\media_revisions_ui\Controller\MediaRevisionController::revisionOverview()

Generates an overview table of older revisions of media.

Parameters

\Drupal\media\MediaInterface $media: A media object.

Return value

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

1 string reference to 'MediaRevisionController::revisionOverview'
media_revisions_ui.routing.yml in ./media_revisions_ui.routing.yml
media_revisions_ui.routing.yml

File

src/Controller/MediaRevisionController.php, line 79

Class

MediaRevisionController
Provides a list of media revisions for a given media.

Namespace

Drupal\media_revisions_ui\Controller

Code

public function revisionOverview(MediaInterface $media) {
  $account = $this
    ->currentUser();
  $langcode = $media
    ->language()
    ->getId();
  $langname = $media
    ->language()
    ->getName();
  $languages = $media
    ->getTranslationLanguages();
  $hasTranslations = count($languages) > 1;

  /** @var \Drupal\media\MediaStorage $mediaStorage */
  $mediaStorage = $this
    ->entityTypeManager()
    ->getStorage('media');
  $title = $this
    ->t('Revisions for %title', [
    '%title' => $media
      ->label(),
  ]);
  if ($hasTranslations) {
    $title = $this
      ->t('@langname revisions for %title', [
      '@langname' => $langname,
      '%title' => $media
        ->label(),
    ]);
  }
  $build['#title'] = $title;
  $header = [
    $this
      ->t('Revision'),
    $this
      ->t('Operations'),
  ];
  $type = $media
    ->bundle();
  $canRevert = $this
    ->accountHasRevertPermission($type, $account) && $media
    ->access('update');
  $canDelete = $this
    ->accountHasDeletePermission($type, $account) && $media
    ->access('delete');
  $rows = [];
  $defaultRevision = $media
    ->getRevisionId();
  $currentRevisionDisplayed = FALSE;
  foreach ($this
    ->getRevisionIds($media, $mediaStorage) as $vid) {

    /** @var \Drupal\media\MediaInterface $revision */
    $revision = $mediaStorage
      ->loadRevision($vid);
    if ($revision
      ->hasTranslation($langcode) && $revision
      ->getTranslation($langcode)
      ->isRevisionTranslationAffected()) {
      $username = [
        '#theme' => 'username',
        '#account' => $revision
          ->getRevisionUser(),
      ];
      $date = $this->dateFormatter
        ->format($revision
        ->getRevisionCreationTime(), 'short');
      $isCurrentRevision = $vid == $defaultRevision || !$currentRevisionDisplayed && $revision
        ->wasDefaultRevision();
      if (!$isCurrentRevision) {
        $link = Link::fromTextAndUrl($date, new Url('entity.media.revision', [
          'media' => $media
            ->id(),
          'media_revision' => $vid,
        ]))
          ->toString();
      }
      else {
        $link = $media
          ->toLink($date)
          ->toString();
        $currentRevisionDisplayed = TRUE;
      }
      $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' => $this->renderer
              ->renderPlain($username),
            'message' => [
              '#markup' => $revision
                ->getRevisionLogMessage(),
              '#allowed_tags' => Xss::getHtmlTagList(),
            ],
          ],
        ],
      ];
      $this->renderer
        ->addCacheableDependency($column['data'], $username);
      $row[] = $column;
      if ($isCurrentRevision) {
        $row[] = [
          'data' => [
            '#prefix' => '<em>',
            '#markup' => $this
              ->t('Current revision'),
            '#suffix' => '</em>',
          ],
        ];
        $rows[] = [
          'data' => $row,
          'class' => [
            'revision-current',
          ],
        ];
      }
      else {
        $links = [];
        if ($canRevert) {
          $revertLink = Url::fromRoute('entity.media.revision_revert_confirm', [
            'media' => $media
              ->id(),
            'media_revision' => $vid,
          ]);
          if ($hasTranslations) {
            $revertLink = Url::fromRoute('entity.media.revision_revert_translation_confirm', [
              'media' => $media
                ->id(),
              'media_revision' => $vid,
              'langcode' => $langcode,
            ]);
          }
          $title = $this
            ->t('Set as current revision');
          if ($vid < $media
            ->getRevisionId()) {
            $title = $this
              ->t('Revert');
          }
          $links['revert'] = [
            'title' => $title,
            'url' => $revertLink,
          ];
        }
        if ($canDelete) {
          $links['delete'] = [
            'title' => $this
              ->t('Delete'),
            'url' => Url::fromRoute('entity.media.revision_delete_confirm', [
              'media' => $media
                ->id(),
              'media_revision' => $vid,
            ]),
          ];
        }
        $row[] = [
          'data' => [
            '#type' => 'operations',
            '#links' => $links,
          ],
        ];
        $rows[] = $row;
      }
    }
  }
  $build['media_revisions_table'] = [
    '#theme' => 'table',
    '#rows' => $rows,
    '#header' => $header,
    '#attached' => [
      'library' => [
        'media_revisions_ui/media_revisions_ui.admin',
      ],
    ],
    '#attributes' => [
      'class' => 'media-revision-table',
    ],
  ];
  $build['pager'] = [
    '#type' => 'pager',
  ];
  return $build;
}