Added Entity and Entity Reference Revisions which got dropped somewhere along the...
[yaffs-website] / web / modules / contrib / entity / src / Controller / RevisionControllerTrait.php
1 <?php
2
3 namespace Drupal\entity\Controller;
4
5 use Drupal\Core\Entity\ContentEntityInterface;
6 use Drupal\Core\Language\LanguageInterface;
7 use Drupal\Core\Entity\EntityInterface;
8
9 /**
10  * Defines a trait for common revision UI functionality.
11  */
12 trait RevisionControllerTrait {
13
14   /**
15    * Returns the entity type manager.
16    *
17    * @return \Drupal\Core\Entity\EntityTypeManagerInterface
18    */
19   abstract protected function entityTypeManager();
20
21   /**
22    * Returns the langauge manager.
23    *
24    * @return \Drupal\Core\Language\LanguageManagerInterface
25    */
26   abstract public function languageManager();
27
28   /**
29    * Determines if the user has permission to revert revisions.
30    *
31    * @param \Drupal\Core\Entity\EntityInterface $entity
32    *   The entity to check revert access for.
33    *
34    * @return bool
35    *   TRUE if the user has revert access.
36    */
37   abstract protected function hasRevertRevisionAccess(EntityInterface $entity);
38
39   /**
40    * Determines if the user has permission to delete revisions.
41    *
42    * @param \Drupal\Core\Entity\EntityInterface $entity
43    *   The entity to check delete revision access for.
44    *
45    * @return bool
46    *   TRUE if the user has delete revision access.
47    */
48   abstract protected function hasDeleteRevisionAccess(EntityInterface $entity);
49
50   /**
51    * Builds a link to revert an entity revision.
52    *
53    * @param \Drupal\Core\Entity\EntityInterface $entity_revision
54    *   The entity to build a revert revision link for.
55    *
56    * @return array
57    *   A link render array.
58    */
59   abstract protected function buildRevertRevisionLink(EntityInterface $entity_revision);
60
61   /**
62    * Builds a link to delete an entity revision.
63    *
64    * @param \Drupal\Core\Entity\EntityInterface $entity_revision
65    *   The entity to build a delete revision link for.
66    *
67    * @return array
68    *   A link render array.
69    */
70   abstract protected function buildDeleteRevisionLink(EntityInterface $entity_revision);
71
72   /**
73    * Returns a string providing details of the revision.
74    *
75    * E.g. Node describes its revisions using {date} by {username}. For the
76    *   non-current revision, it also provides a link to view that revision.
77    *
78    * @param \Drupal\Core\Entity\ContentEntityInterface $revision
79    *   The entity revision.
80    * @param bool $is_current
81    *   TRUE if the revision is the current revision.
82    *
83    * @return string
84    *   Returns a string to provide the details of the revision.
85    */
86   abstract protected function getRevisionDescription(ContentEntityInterface $revision, $is_current = FALSE);
87
88   /**
89    * Loads all revision IDs of an entity sorted by revision ID descending.
90    *
91    * @param \Drupal\Core\Entity\ContentEntityInterface $entity
92    *   The entity.
93    *
94    * @return mixed[]
95    */
96   protected function revisionIds(ContentEntityInterface $entity) {
97     $entity_type = $entity->getEntityType();
98     $result = $this->entityTypeManager()->getStorage($entity_type->id())->getQuery()
99       ->allRevisions()
100       ->condition($entity_type->getKey('id'), $entity->id())
101       ->sort($entity_type->getKey('revision'), 'DESC')
102       ->execute();
103     return array_keys($result);
104   }
105
106   /**
107    * Generates an overview table of older revisions of an entity.
108    *
109    * @param \Drupal\Core\Entity\ContentEntityInterface $entity
110    *   An entity object.
111    *
112    * @return array
113    *   A render array.
114    */
115   protected function revisionOverview(ContentEntityInterface $entity) {
116     $langcode = $this->languageManager()
117       ->getCurrentLanguage(LanguageInterface::TYPE_CONTENT)
118       ->getId();
119     /** @var \Drupal\Core\Entity\ContentEntityStorageInterface $entity_storage */
120     $entity_storage = $this->entityTypeManager()->getStorage($entity->getEntityTypeId());
121     $revision_ids = $this->revisionIds($entity);
122     $entity_revisions = $entity_storage->loadMultipleRevisions($revision_ids);
123
124     $header = [$this->t('Revision'), $this->t('Operations')];
125     $rows = [];
126     foreach ($entity_revisions as $revision) {
127       $row = [];
128       /** @var \Drupal\Core\Entity\ContentEntityInterface $revision */
129       if ($revision->hasTranslation($langcode) && $revision->getTranslation($langcode)->isRevisionTranslationAffected()) {
130         $row[] = $this->getRevisionDescription($revision, $revision->isDefaultRevision());
131
132         if ($revision->isDefaultRevision()) {
133           $row[] = [
134             'data' => [
135               '#prefix' => '<em>',
136               '#markup' => $this->t('Current revision'),
137               '#suffix' => '</em>',
138             ],
139           ];
140           foreach ($row as &$current) {
141             $current['class'] = ['revision-current'];
142           }
143         }
144         else {
145           $links = $this->getOperationLinks($revision);
146           $row[] = [
147             'data' => [
148               '#type' => 'operations',
149               '#links' => $links,
150             ],
151           ];
152         }
153       }
154
155       $rows[] = $row;
156     }
157
158     $build[$entity->getEntityTypeId() . '_revisions_table'] = [
159       '#theme' => 'table',
160       '#rows' => $rows,
161       '#header' => $header,
162     ];
163
164     // We have no clue about caching yet.
165     $build['#cache']['max-age'] = 0;
166
167     return $build;
168   }
169
170   /**
171    * Get the links of the operations for an entity revision.
172    *
173    * @param \Drupal\Core\Entity\EntityInterface $entity_revision
174    *   The entity to build the revision links for.
175    *
176    * @return array
177    *   The operation links.
178    */
179   protected function getOperationLinks(EntityInterface $entity_revision) {
180     $links = [];
181     if ($this->hasRevertRevisionAccess($entity_revision)) {
182       $links['revert'] = $this->buildRevertRevisionLink($entity_revision);
183     }
184
185     if ($this->hasDeleteRevisionAccess($entity_revision)) {
186       $links['delete'] = $this->buildDeleteRevisionLink($entity_revision);
187     }
188
189     return array_filter($links);
190   }
191
192 }