View source
<?php
namespace Drupal\media_revisions_ui\Controller;
use Drupal\Component\Utility\Xss;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Datetime\DateFormatterInterface;
use Drupal\Core\Entity\EntityRepositoryInterface;
use Drupal\Core\Link;
use Drupal\Core\Render\RendererInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Url;
use Drupal\media\MediaInterface;
use Drupal\media\MediaStorage;
use Symfony\Component\DependencyInjection\ContainerInterface;
class MediaRevisionController extends ControllerBase {
protected $dateFormatter;
protected $renderer;
protected $entityRepository;
public function __construct(DateFormatterInterface $date_formatter, RendererInterface $renderer, EntityRepositoryInterface $entity_repository = NULL) {
$this->dateFormatter = $date_formatter;
$this->renderer = $renderer;
$this->entityRepository = $entity_repository;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('date.formatter'), $container
->get('renderer'), $container
->get('entity.repository'));
}
public function revisionOverview(MediaInterface $media) {
$account = $this
->currentUser();
$langcode = $media
->language()
->getId();
$langname = $media
->language()
->getName();
$languages = $media
->getTranslationLanguages();
$hasTranslations = count($languages) > 1;
$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) {
$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;
}
protected function getRevisionIds(MediaInterface $media, MediaStorage $mediaStorage) {
$result = $mediaStorage
->getQuery()
->allRevisions()
->condition('mid', $media
->id())
->sort('vid', 'DESC')
->pager(50)
->execute();
return array_keys($result);
}
protected function accountHasRevertPermission($mediaType, AccountInterface $account) {
$hasRevertPermission = FALSE;
$revertPermissions = [
"revert {$mediaType} media revisions",
'revert all media revisions',
'administer media',
];
foreach ($revertPermissions as $permission) {
if ($account
->hasPermission($permission)) {
$hasRevertPermission = TRUE;
break;
}
}
return $hasRevertPermission;
}
protected function accountHasDeletePermission($mediaType, AccountInterface $account) {
$hasRevertPermission = FALSE;
$revertPermissions = [
"delete {$mediaType} media revisions",
'delete all media revisions',
'administer media',
];
foreach ($revertPermissions as $permission) {
if ($account
->hasPermission($permission)) {
$hasRevertPermission = TRUE;
break;
}
}
return $hasRevertPermission;
}
}