502a67cb8c92ada3e670778a920cdc9938b1170d
[yaffs-website] / web / modules / contrib / entity / src / Controller / RevisionOverviewController.php
1 <?php
2
3 namespace Drupal\entity\Controller;
4
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;
16
17 /**
18  * Provides a controller which shows the revision history.
19  *
20  * This controller leverages the revision controller trait, which is agnostic to
21  * any entity type, by using \Drupal\Core\Entity\RevisionLogInterface.
22  */
23 class RevisionOverviewController extends ControllerBase {
24
25   use RevisionControllerTrait;
26
27   /**
28    * The date formatter.
29    *
30    * @var \Drupal\Core\Datetime\DateFormatterInterface
31    */
32   protected $dateFormatter;
33
34   /**
35    * The renderer.
36    *
37    * @var \Drupal\Core\Render\RendererInterface
38    */
39   protected $renderer;
40
41   /**
42    * Creates a new RevisionOverviewController instance.
43    *
44    * @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter
45    *   The date formatter.
46    */
47   public function __construct(DateFormatterInterface $date_formatter, RendererInterface $renderer) {
48     $this->dateFormatter = $date_formatter;
49     $this->renderer = $renderer;
50   }
51
52   /**
53    * {@inheritdoc}
54    */
55   public static function create(ContainerInterface $container) {
56     return new static($container->get('date.formatter'), $container->get('renderer'));
57   }
58
59   /**
60    * {@inheritdoc}
61    */
62   protected function hasDeleteRevisionAccess(EntityInterface $entity) {
63     return $this->currentUser()->hasPermission("delete all {$entity->id()} revisions");
64   }
65
66   /**
67    * {@inheritdoc}
68    */
69   protected function buildRevertRevisionLink(EntityInterface $entity_revision) {
70     if ($entity_revision->hasLinkTemplate('revision-revert-form')) {
71       return [
72         'title' => t('Revert'),
73         'url' => $entity_revision->toUrl('revision-revert-form'),
74       ];
75     }
76   }
77
78   /**
79    * {@inheritdoc}
80    */
81   protected function buildDeleteRevisionLink(EntityInterface $entity_revision) {
82     if ($entity_revision->hasLinkTemplate('revision-delete-form')) {
83       return [
84         'title' => t('Delete'),
85         'url' => $entity_revision->toUrl('revision-delete-form'),
86       ];
87     }
88   }
89
90   /**
91    * Generates an overview table of older revisions of an entity.
92    *
93    * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
94    *   The route match.
95    *
96    * @return array
97    *   A render array.
98    */
99   public function revisionOverviewController(RouteMatchInterface $route_match) {
100     return $this->revisionOverview($route_match->getParameter($route_match->getRouteObject()->getOption('entity_type_id')));
101   }
102
103   /**
104    * {@inheritdoc}
105    */
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');
112
113       // @todo: Simplify this when https://www.drupal.org/node/2334319 lands.
114       $username = [
115         '#theme' => 'username',
116         '#account' => $revision->getRevisionUser(),
117       ];
118       $username = $this->renderer->render($username);
119     }
120     else {
121       $link = $revision->toLink($revision->label(), 'revision');
122       $username = '';
123
124     }
125
126     $markup = '';
127     if ($revision instanceof RevisionLogInterface) {
128       $markup = $revision->getRevisionLogMessage();
129     }
130
131     if ($username) {
132       $template = '{% trans %}{{ date }} by {{ username }}{% endtrans %}{% if message %}<p class="revision-log">{{ message }}</p>{% endif %}';
133     }
134     else {
135       $template = '{% trans %} {{ date }} {% endtrans %}{% if message %}<p class="revision-log">{{ message }}</p>{% endif %}';
136     }
137
138     $column = [
139       'data' => [
140         '#type' => 'inline_template',
141         '#template' => $template,
142         '#context' => [
143           'date' => $link->toString(),
144           'username' => $username,
145           'message' => ['#markup' => $markup, '#allowed_tags' => Xss::getHtmlTagList()],
146         ],
147       ],
148     ];
149     return $column;
150   }
151
152   /**
153    * {@inheritdoc}
154    */
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")
158     );
159   }
160
161 }