3 namespace Drupal\entity\Controller;
5 use Drupal\Component\Utility\Xss;
6 use Drupal\Core\Access\AccessResult;
7 use Drupal\Core\Controller\ControllerBase;
8 use Drupal\Core\Datetime\DateFormatterInterface;
9 use Drupal\Core\Entity\ContentEntityInterface;
10 use Drupal\Core\Entity\EntityInterface;
11 use Drupal\Core\Render\RendererInterface;
12 use Drupal\Core\Routing\RouteMatchInterface;
13 use Drupal\Core\Entity\RevisionLogInterface;
14 use Drupal\user\EntityOwnerInterface;
15 use Symfony\Component\DependencyInjection\ContainerInterface;
18 * Provides a controller which shows the revision history.
20 * This controller leverages the revision controller trait, which is agnostic to
21 * any entity type, by using \Drupal\Core\Entity\RevisionLogInterface.
23 class RevisionOverviewController extends ControllerBase {
25 use RevisionControllerTrait;
30 * @var \Drupal\Core\Datetime\DateFormatterInterface
32 protected $dateFormatter;
37 * @var \Drupal\Core\Render\RendererInterface
42 * Creates a new RevisionOverviewController instance.
44 * @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter
47 public function __construct(DateFormatterInterface $date_formatter, RendererInterface $renderer) {
48 $this->dateFormatter = $date_formatter;
49 $this->renderer = $renderer;
55 public static function create(ContainerInterface $container) {
56 return new static($container->get('date.formatter'), $container->get('renderer'));
62 protected function hasDeleteRevisionAccess(EntityInterface $entity) {
63 return $this->currentUser()->hasPermission("delete all {$entity->id()} revisions");
69 protected function buildRevertRevisionLink(EntityInterface $entity_revision) {
70 if ($entity_revision->hasLinkTemplate('revision-revert-form')) {
72 'title' => t('Revert'),
73 'url' => $entity_revision->toUrl('revision-revert-form'),
81 protected function buildDeleteRevisionLink(EntityInterface $entity_revision) {
82 if ($entity_revision->hasLinkTemplate('revision-delete-form')) {
84 'title' => t('Delete'),
85 'url' => $entity_revision->toUrl('revision-delete-form'),
91 * Generates an overview table of older revisions of an entity.
93 * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
99 public function revisionOverviewController(RouteMatchInterface $route_match) {
100 return $this->revisionOverview($route_match->getParameter($route_match->getRouteObject()->getOption('entity_type_id')));
106 protected function getRevisionDescription(ContentEntityInterface $revision, $is_default = FALSE) {
107 /** @var \Drupal\Core\Entity\ContentEntityInterface|\Drupal\user\EntityOwnerInterface|\Drupal\Core\Entity\RevisionLogInterface $revision */
108 if ($revision instanceof RevisionLogInterface) {
109 // Use revision link to link to revisions that are not active.
110 $date = $this->dateFormatter->format($revision->getRevisionCreationTime(), 'short');
111 $link = $revision->toLink($date, 'revision');
113 // @todo: Simplify this when https://www.drupal.org/node/2334319 lands.
115 '#theme' => 'username',
116 '#account' => $revision->getRevisionUser(),
118 $username = $this->renderer->render($username);
121 $link = $revision->toLink($revision->label(), 'revision');
127 if ($revision instanceof RevisionLogInterface) {
128 $markup = $revision->getRevisionLogMessage();
132 $template = '{% trans %}{{ date }} by {{ username }}{% endtrans %}{% if message %}<p class="revision-log">{{ message }}</p>{% endif %}';
135 $template = '{% trans %} {{ date }} {% endtrans %}{% if message %}<p class="revision-log">{{ message }}</p>{% endif %}';
140 '#type' => 'inline_template',
141 '#template' => $template,
143 'date' => $link->toString(),
144 'username' => $username,
145 'message' => ['#markup' => $markup, '#allowed_tags' => Xss::getHtmlTagList()],
155 protected function hasRevertRevisionAccess(EntityInterface $entity) {
156 return AccessResult::allowedIfHasPermission($this->currentUser(), "revert all {$entity->getEntityTypeId()} revisions")->orIf(
157 AccessResult::allowedIfHasPermission($this->currentUser(), "revert {$entity->bundle()} {$entity->getEntityTypeId()} revisions")