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 Symfony\Component\DependencyInjection\ContainerInterface;
17 * Provides a controller which shows the revision history.
19 * This controller leverages the revision controller trait, which is agnostic to
20 * any entity type, by using \Drupal\Core\Entity\RevisionLogInterface.
22 class RevisionOverviewController extends ControllerBase {
24 use RevisionControllerTrait;
29 * @var \Drupal\Core\Datetime\DateFormatterInterface
31 protected $dateFormatter;
36 * @var \Drupal\Core\Render\RendererInterface
41 * Creates a new RevisionOverviewController instance.
43 * @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter
46 public function __construct(DateFormatterInterface $date_formatter, RendererInterface $renderer) {
47 $this->dateFormatter = $date_formatter;
48 $this->renderer = $renderer;
54 public static function create(ContainerInterface $container) {
55 return new static($container->get('date.formatter'), $container->get('renderer'));
61 protected function hasDeleteRevisionAccess(EntityInterface $entity) {
62 return $this->currentUser()->hasPermission("delete all {$entity->id()} revisions");
68 protected function buildRevertRevisionLink(EntityInterface $entity_revision) {
69 if ($entity_revision->hasLinkTemplate('revision-revert-form')) {
71 'title' => t('Revert'),
72 'url' => $entity_revision->toUrl('revision-revert-form'),
80 protected function buildDeleteRevisionLink(EntityInterface $entity_revision) {
81 if ($entity_revision->hasLinkTemplate('revision-delete-form')) {
83 'title' => t('Delete'),
84 'url' => $entity_revision->toUrl('revision-delete-form'),
90 * Generates an overview table of older revisions of an entity.
92 * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
98 public function revisionOverviewController(RouteMatchInterface $route_match) {
99 return $this->revisionOverview($route_match->getParameter($route_match->getRouteObject()->getOption('entity_type_id')));
105 protected function getRevisionDescription(ContentEntityInterface $revision, $is_default = FALSE) {
106 /** @var \Drupal\Core\Entity\ContentEntityInterface|\Drupal\user\EntityOwnerInterface|\Drupal\Core\Entity\RevisionLogInterface $revision */
107 if ($revision instanceof RevisionLogInterface) {
108 // Use revision link to link to revisions that are not active.
109 $date = $this->dateFormatter->format($revision->getRevisionCreationTime(), 'short');
110 $link = $revision->toLink($date, 'revision');
112 // @todo: Simplify this when https://www.drupal.org/node/2334319 lands.
114 '#theme' => 'username',
115 '#account' => $revision->getRevisionUser(),
117 $username = $this->renderer->render($username);
120 $link = $revision->toLink($revision->label(), 'revision');
126 if ($revision instanceof RevisionLogInterface) {
127 $markup = $revision->getRevisionLogMessage();
131 $template = '{% trans %}{{ date }} by {{ username }}{% endtrans %}{% if message %}<p class="revision-log">{{ message }}</p>{% endif %}';
134 $template = '{% trans %} {{ date }} {% endtrans %}{% if message %}<p class="revision-log">{{ message }}</p>{% endif %}';
139 '#type' => 'inline_template',
140 '#template' => $template,
142 'date' => $link->toString(),
143 'username' => $username,
144 'message' => ['#markup' => $markup, '#allowed_tags' => Xss::getHtmlTagList()],
154 protected function hasRevertRevisionAccess(EntityInterface $entity) {
155 return AccessResult::allowedIfHasPermission($this->currentUser(), "revert all {$entity->getEntityTypeId()} revisions")->orIf(
156 AccessResult::allowedIfHasPermission($this->currentUser(), "revert {$entity->bundle()} {$entity->getEntityTypeId()} revisions")