3 namespace Drupal\entity\Controller;
5 use Drupal\Core\Entity\ContentEntityInterface;
6 use Drupal\Core\Language\LanguageInterface;
7 use Drupal\Core\Entity\EntityInterface;
10 * Defines a trait for common revision UI functionality.
12 trait RevisionControllerTrait {
15 * Returns the entity type manager.
17 * @return \Drupal\Core\Entity\EntityTypeManagerInterface
19 abstract protected function entityTypeManager();
22 * Returns the langauge manager.
24 * @return \Drupal\Core\Language\LanguageManagerInterface
26 abstract public function languageManager();
29 * Determines if the user has permission to revert revisions.
31 * @param \Drupal\Core\Entity\EntityInterface $entity
32 * The entity to check revert access for.
35 * TRUE if the user has revert access.
37 abstract protected function hasRevertRevisionAccess(EntityInterface $entity);
40 * Determines if the user has permission to delete revisions.
42 * @param \Drupal\Core\Entity\EntityInterface $entity
43 * The entity to check delete revision access for.
46 * TRUE if the user has delete revision access.
48 abstract protected function hasDeleteRevisionAccess(EntityInterface $entity);
51 * Builds a link to revert an entity revision.
53 * @param \Drupal\Core\Entity\EntityInterface $entity_revision
54 * The entity to build a revert revision link for.
57 * A link render array.
59 abstract protected function buildRevertRevisionLink(EntityInterface $entity_revision);
62 * Builds a link to delete an entity revision.
64 * @param \Drupal\Core\Entity\EntityInterface $entity_revision
65 * The entity to build a delete revision link for.
68 * A link render array.
70 abstract protected function buildDeleteRevisionLink(EntityInterface $entity_revision);
73 * Returns a string providing details of the revision.
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.
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.
84 * Returns a string to provide the details of the revision.
86 abstract protected function getRevisionDescription(ContentEntityInterface $revision, $is_current = FALSE);
89 * Loads all revision IDs of an entity sorted by revision ID descending.
91 * @param \Drupal\Core\Entity\ContentEntityInterface $entity
96 protected function revisionIds(ContentEntityInterface $entity) {
97 $entity_type = $entity->getEntityType();
98 $result = $this->entityTypeManager()->getStorage($entity_type->id())->getQuery()
100 ->condition($entity_type->getKey('id'), $entity->id())
101 ->sort($entity_type->getKey('revision'), 'DESC')
103 return array_keys($result);
107 * Generates an overview table of older revisions of an entity.
109 * @param \Drupal\Core\Entity\ContentEntityInterface $entity
115 protected function revisionOverview(ContentEntityInterface $entity) {
116 $langcode = $this->languageManager()
117 ->getCurrentLanguage(LanguageInterface::TYPE_CONTENT)
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);
124 $header = [$this->t('Revision'), $this->t('Operations')];
126 foreach ($entity_revisions as $revision) {
128 /** @var \Drupal\Core\Entity\ContentEntityInterface $revision */
129 if ($revision->hasTranslation($langcode) && $revision->getTranslation($langcode)->isRevisionTranslationAffected()) {
130 $row[] = $this->getRevisionDescription($revision, $revision->isDefaultRevision());
132 if ($revision->isDefaultRevision()) {
136 '#markup' => $this->t('Current revision'),
137 '#suffix' => '</em>',
140 foreach ($row as &$current) {
141 $current['class'] = ['revision-current'];
145 $links = $this->getOperationLinks($revision);
148 '#type' => 'operations',
158 $build[$entity->getEntityTypeId() . '_revisions_table'] = [
161 '#header' => $header,
164 // We have no clue about caching yet.
165 $build['#cache']['max-age'] = 0;
171 * Get the links of the operations for an entity revision.
173 * @param \Drupal\Core\Entity\EntityInterface $entity_revision
174 * The entity to build the revision links for.
177 * The operation links.
179 protected function getOperationLinks(EntityInterface $entity_revision) {
181 if ($this->hasRevertRevisionAccess($entity_revision)) {
182 $links['revert'] = $this->buildRevertRevisionLink($entity_revision);
185 if ($this->hasDeleteRevisionAccess($entity_revision)) {
186 $links['delete'] = $this->buildDeleteRevisionLink($entity_revision);
189 return array_filter($links);