AitisiEntityController.php 5.74 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
<?php

namespace Drupal\eiek\Controller;

use Drupal\Component\Utility\Xss;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Url;
use Drupal\eiek\Entity\AitisiEntityInterface;

/**
 * Class AitisiEntityController.
 *
 *  Returns responses for Aitisi entity routes.
 *
 * @package Drupal\eiek\Controller
 */
class AitisiEntityController extends ControllerBase implements ContainerInjectionInterface {

  /**
   * Displays a Aitisi entity  revision.
   *
   * @param int $aitisi_entity_revision
   *   The Aitisi entity  revision ID.
   *
   * @return array
   *   An array suitable for drupal_render().
   */
  public function revisionShow($aitisi_entity_revision) {
    $aitisi_entity = $this->entityManager()->getStorage('aitisi_entity')->loadRevision($aitisi_entity_revision);
    $view_builder = $this->entityManager()->getViewBuilder('aitisi_entity');

    return $view_builder->view($aitisi_entity);
  }

  /**
   * Page title callback for a Aitisi entity  revision.
   *
   * @param int $aitisi_entity_revision
   *   The Aitisi entity  revision ID.
   *
   * @return string
   *   The page title.
   */
  public function revisionPageTitle($aitisi_entity_revision) {
    $aitisi_entity = $this->entityManager()->getStorage('aitisi_entity')->loadRevision($aitisi_entity_revision);
    return $this->t('Revision of %title from %date', array('%title' => $aitisi_entity->label(), '%date' => format_date($aitisi_entity->getRevisionCreationTime())));
  }

  /**
   * Generates an overview table of older revisions of a Aitisi entity .
   *
   * @param \Drupal\eiek\Entity\AitisiEntityInterface $aitisi_entity
   *   A Aitisi entity  object.
   *
   * @return array
   *   An array as expected by drupal_render().
   */
  public function revisionOverview(AitisiEntityInterface $aitisi_entity) {
    $account = $this->currentUser();
    $langcode = $aitisi_entity->language()->getId();
    $langname = $aitisi_entity->language()->getName();
    $languages = $aitisi_entity->getTranslationLanguages();
    $has_translations = (count($languages) > 1);
    $aitisi_entity_storage = $this->entityManager()->getStorage('aitisi_entity');

    $build['#title'] = $has_translations ? $this->t('@langname revisions for %title', ['@langname' => $langname, '%title' => $aitisi_entity->label()]) : $this->t('Revisions for %title', ['%title' => $aitisi_entity->label()]);
    $header = array($this->t('Revision'), $this->t('Operations'));

    $revert_permission = (($account->hasPermission("revert all aitisi entity revisions") || $account->hasPermission('administer aitisi entity entities')));
    $delete_permission = (($account->hasPermission("delete all aitisi entity revisions") || $account->hasPermission('administer aitisi entity entities')));

    $rows = array();

    $vids = $aitisi_entity_storage->revisionIds($aitisi_entity);

    $latest_revision = TRUE;

    foreach (array_reverse($vids) as $vid) {
      /** @var \Drupal\eiek\AitisiEntityInterface $revision */
      $revision = $aitisi_entity_storage->loadRevision($vid);
      // Only show revisions that are affected by the language that is being
      // displayed.
      if ($revision->hasTranslation($langcode) && $revision->getTranslation($langcode)->isRevisionTranslationAffected()) {
        $username = [
          '#theme' => 'username',
          '#account' => $revision->getRevisionUser(),
        ];

        // Use revision link to link to revisions that are not active.
        $date = \Drupal::service('date.formatter')->format($revision->revision_timestamp->value, 'short');
        if ($vid != $aitisi_entity->getRevisionId()) {
          $link = $this->l($date, new Url('entity.aitisi_entity.revision', ['aitisi_entity' => $aitisi_entity->id(), 'aitisi_entity_revision' => $vid]));
        }
        else {
          $link = $aitisi_entity->link($date);
        }

        $row = [];
        $column = [
          'data' => [
            '#type' => 'inline_template',
            '#template' => '{% trans %}{{ date }} by {{ username }}{% endtrans %}{% if message %}<p class="revision-log">{{ message }}</p>{% endif %}',
            '#context' => [
              'date' => $link,
              'username' => \Drupal::service('renderer')->renderPlain($username),
              'message' => ['#markup' => $revision->revision_log_message->value, '#allowed_tags' => Xss::getHtmlTagList()],
            ],
          ],
        ];
        $row[] = $column;

        if ($latest_revision) {
          $row[] = [
            'data' => [
              '#prefix' => '<em>',
              '#markup' => $this->t('Current revision'),
              '#suffix' => '</em>',
            ],
          ];
          foreach ($row as &$current) {
            $current['class'] = ['revision-current'];
          }
          $latest_revision = FALSE;
        }
        else {
          $links = [];
          if ($revert_permission) {
            $links['revert'] = [
              'title' => $this->t('Revert'),
              'url' => Url::fromRoute('aitisi_entity.revision_revert_confirm', ['aitisi_entity' => $aitisi_entity->id(), 'aitisi_entity_revision' => $vid]),
            ];
          }

          if ($delete_permission) {
            $links['delete'] = [
              'title' => $this->t('Delete'),
              'url' => Url::fromRoute('aitisi_entity.revision_delete_confirm', ['aitisi_entity' => $aitisi_entity->id(), 'aitisi_entity_revision' => $vid]),
            ];
          }

          $row[] = [
            'data' => [
              '#type' => 'operations',
              '#links' => $links,
            ],
          ];
        }

        $rows[] = $row;
      }
    }

    $build['aitisi_entity_revisions_table'] = array(
      '#theme' => 'table',
      '#rows' => $rows,
      '#header' => $header,
    );

    return $build;
  }

}