9751b17ae0d48ea240050dfb7c89d8ccb5094b1e
[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   public abstract 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    */
60   abstract protected function buildRevertRevisionLink(EntityInterface $entity_revision);
61
62   /**
63    * Builds a link to delete an entity revision.
64    *
65    * @param \Drupal\Core\Entity\EntityInterface $entity_revision
66    *   The entity to build a delete revision link for.
67    *
68    * @return array
69    *   A link render array.
70    */
71   abstract protected function buildDeleteRevisionLink(EntityInterface $entity_revision);
72
73   /**
74    * Returns a string providing details of the revision.
75    *
76    * E.g. Node describes its revisions using {date} by {username}. For the
77    *   non-current revision, it also provides a link to view that revision.
78    *
79    * @param \Drupal\Core\Entity\ContentEntityInterface $revision
80    *   The entity revision.
81    * @param bool $is_current
82    *   TRUE if the revision is the current revision.
83    *
84    * @return string
85    *   Returns a string to provide the details of the revision.
86    */
87   abstract protected function getRevisionDescription(ContentEntityInterface $revision, $is_current = FALSE);
88
89   /**
90    * Loads all revision IDs of an entity sorted by revision ID descending.
91    *
92    * @param \Drupal\Core\Entity\ContentEntityInterface $entity
93    *   The entity.
94    *
95    * @return mixed[]
96    */
97   protected function revisionIds(ContentEntityInterface $entity) {
98     $entity_type = $entity->getEntityType();
99     $result = $this->entityTypeManager()->getStorage($entity_type->id())->getQuery()
100       ->allRevisions()
101       ->condition($entity_type->getKey('id'), $entity->id())
102       ->sort($entity_type->getKey('revision'), 'DESC')
103       ->execute();
104     return array_keys($result);
105   }
106
107   /**
108    * Generates an overview table of older revisions of an entity.
109    *
110    * @param \Drupal\Core\Entity\ContentEntityInterface $entity
111    *   An entity object.
112    *
113    * @return array
114    *   A render array.
115    */
116   protected function revisionOverview(ContentEntityInterface $entity) {
117     $langcode = $this->languageManager()
118       ->getCurrentLanguage(LanguageInterface::TYPE_CONTENT)
119       ->getId();
120     $entity_storage = $this->entityTypeManager()
121       ->getStorage($entity->getEntityTypeId());
122
123     $header = [$this->t('Revision'), $this->t('Operations')];
124     $rows = [];
125
126     $revision_ids = $this->revisionIds($entity);
127     // @todo Expand the entity storage to load multiple revisions.
128     $entity_revisions = array_combine($revision_ids, array_map(function($vid) use ($entity_storage) {
129       return $entity_storage->loadRevision($vid);
130       }, $revision_ids));
131
132     foreach ($entity_revisions as $revision) {
133       $row = [];
134       /** @var \Drupal\Core\Entity\ContentEntityInterface $revision */
135       if ($revision->hasTranslation($langcode) && $revision->getTranslation($langcode)
136           ->isRevisionTranslationAffected()
137       ) {
138         $row[] = $this->getRevisionDescription($revision, $revision->isDefaultRevision());
139
140         if ($revision->isDefaultRevision()) {
141           $row[] = [
142             'data' => [
143               '#prefix' => '<em>',
144               '#markup' => $this->t('Current revision'),
145               '#suffix' => '</em>',
146             ],
147           ];
148           foreach ($row as &$current) {
149             $current['class'] = ['revision-current'];
150           }
151         }
152         else {
153           $links = $this->getOperationLinks($revision);
154           $row[] = [
155             'data' => [
156               '#type' => 'operations',
157               '#links' => $links,
158             ],
159           ];
160         }
161       }
162
163       $rows[] = $row;
164     }
165
166     $build[$entity->getEntityTypeId() . '_revisions_table'] = [
167       '#theme' => 'table',
168       '#rows' => $rows,
169       '#header' => $header,
170     ];
171
172     // We have no clue about caching yet.
173     $build['#cache']['max-age'] = 0;
174
175     return $build;
176   }
177
178   /**
179    * Get the links of the operations for an entity revision.
180    *
181    * @param \Drupal\Core\Entity\EntityInterface $entity_revision
182    *   The entity to build the revision links for.
183    *
184    * @return array
185    *   The operation links.
186    */
187   protected function getOperationLinks(EntityInterface $entity_revision) {
188     $links = [];
189     if ($this->hasRevertRevisionAccess($entity_revision)) {
190       $links['revert'] = $this->buildRevertRevisionLink($entity_revision);
191     }
192
193     if ($this->hasDeleteRevisionAccess($entity_revision)) {
194       $links['delete'] = $this->buildDeleteRevisionLink($entity_revision);
195     }
196
197     return array_filter($links);
198   }
199
200 }