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