RevisionOverviewController.php in Entity API 8
File
src/Controller/RevisionOverviewController.php
View source
<?php
namespace Drupal\entity\Controller;
use Drupal\Component\Utility\Xss;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Datetime\DateFormatterInterface;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Render\RendererInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Entity\RevisionLogInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class RevisionOverviewController extends ControllerBase {
use RevisionControllerTrait;
protected $dateFormatter;
protected $renderer;
public function __construct(DateFormatterInterface $date_formatter, RendererInterface $renderer) {
$this->dateFormatter = $date_formatter;
$this->renderer = $renderer;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('date.formatter'), $container
->get('renderer'));
}
protected function hasDeleteRevisionAccess(EntityInterface $entity) {
return $this
->currentUser()
->hasPermission("delete all {$entity->getEntityTypeId()} revisions");
}
protected function buildRevertRevisionLink(EntityInterface $entity_revision) {
if ($entity_revision
->hasLinkTemplate('revision-revert-form')) {
return [
'title' => $this
->t('Revert'),
'url' => $entity_revision
->toUrl('revision-revert-form'),
];
}
}
protected function buildDeleteRevisionLink(EntityInterface $entity_revision) {
if ($entity_revision
->hasLinkTemplate('revision-delete-form')) {
return [
'title' => $this
->t('Delete'),
'url' => $entity_revision
->toUrl('revision-delete-form'),
];
}
}
public function revisionOverviewController(RouteMatchInterface $route_match) {
return $this
->revisionOverview($route_match
->getParameter($route_match
->getRouteObject()
->getOption('entity_type_id')));
}
protected function getRevisionDescription(ContentEntityInterface $revision, $is_default = FALSE) {
if ($revision instanceof RevisionLogInterface) {
$date = $this->dateFormatter
->format($revision
->getRevisionCreationTime(), 'short');
$link = $revision
->toLink($date, 'revision');
$username = [
'#theme' => 'username',
'#account' => $revision
->getRevisionUser(),
];
$username = $this->renderer
->render($username);
}
else {
$link = $revision
->toLink($revision
->label(), 'revision');
$username = '';
}
$markup = '';
if ($revision instanceof RevisionLogInterface) {
$markup = $revision
->getRevisionLogMessage();
}
if ($username) {
$template = '{% trans %}{{ date }} by {{ username }}{% endtrans %}{% if message %}<p class="revision-log">{{ message }}</p>{% endif %}';
}
else {
$template = '{% trans %} {{ date }} {% endtrans %}{% if message %}<p class="revision-log">{{ message }}</p>{% endif %}';
}
$column = [
'data' => [
'#type' => 'inline_template',
'#template' => $template,
'#context' => [
'date' => $link
->toString(),
'username' => $username,
'message' => [
'#markup' => $markup,
'#allowed_tags' => Xss::getHtmlTagList(),
],
],
],
];
return $column;
}
protected function hasRevertRevisionAccess(EntityInterface $entity) {
return AccessResult::allowedIfHasPermission($this
->currentUser(), "revert all {$entity->getEntityTypeId()} revisions")
->orIf(AccessResult::allowedIfHasPermission($this
->currentUser(), "revert {$entity->bundle()} {$entity->getEntityTypeId()} revisions"));
}
}