HeartbeatController.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. namespace Drupal\heartbeat8\Controller;
  3. use Drupal\Component\Utility\Xss;
  4. use Drupal\Core\Controller\ControllerBase;
  5. use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
  6. use Drupal\Core\Url;
  7. use Drupal\heartbeat8\Entity\HeartbeatInterface;
  8. /**
  9. * Class HeartbeatController.
  10. *
  11. * Returns responses for Heartbeat routes.
  12. *
  13. * @package Drupal\heartbeat8\Controller
  14. */
  15. class HeartbeatController extends ControllerBase implements ContainerInjectionInterface {
  16. /**
  17. * Displays a Heartbeat revision.
  18. *
  19. * @param int $heartbeat_revision
  20. * The Heartbeat revision ID.
  21. *
  22. * @return array
  23. * An array suitable for drupal_render().
  24. */
  25. public function revisionShow($heartbeat_revision) {
  26. $heartbeat = $this->entityManager()->getStorage('heartbeat')->loadRevision($heartbeat_revision);
  27. $view_builder = $this->entityManager()->getViewBuilder('heartbeat');
  28. return $view_builder->view($heartbeat);
  29. }
  30. /**
  31. * Page title callback for a Heartbeat revision.
  32. *
  33. * @param int $heartbeat_revision
  34. * The Heartbeat revision ID.
  35. *
  36. * @return string
  37. * The page title.
  38. */
  39. public function revisionPageTitle($heartbeat_revision) {
  40. $heartbeat = $this->entityManager()->getStorage('heartbeat')->loadRevision($heartbeat_revision);
  41. return $this->t('Revision of %title from %date', array('%title' => $heartbeat->label(), '%date' => format_date($heartbeat->getRevisionCreationTime())));
  42. }
  43. /**
  44. * Generates an overview table of older revisions of a Heartbeat .
  45. *
  46. * @param \Drupal\heartbeat8\Entity\HeartbeatInterface $heartbeat
  47. * A Heartbeat object.
  48. *
  49. * @return array
  50. * An array as expected by drupal_render().
  51. */
  52. public function revisionOverview(HeartbeatInterface $heartbeat) {
  53. $account = $this->currentUser();
  54. $langcode = $heartbeat->language()->getId();
  55. $langname = $heartbeat->language()->getName();
  56. $languages = $heartbeat->getTranslationLanguages();
  57. $has_translations = (count($languages) > 1);
  58. $heartbeat_storage = $this->entityManager()->getStorage('heartbeat');
  59. $build['#title'] = $has_translations ? $this->t('@langname revisions for %title', ['@langname' => $langname, '%title' => $heartbeat->label()]) : $this->t('Revisions for %title', ['%title' => $heartbeat->label()]);
  60. $header = array($this->t('Revision'), $this->t('Operations'));
  61. $revert_permission = (($account->hasPermission("revert all heartbeat revisions") || $account->hasPermission('administer heartbeat entities')));
  62. $delete_permission = (($account->hasPermission("delete all heartbeat revisions") || $account->hasPermission('administer heartbeat entities')));
  63. $rows = array();
  64. $vids = $heartbeat_storage->revisionIds($heartbeat);
  65. $latest_revision = TRUE;
  66. foreach (array_reverse($vids) as $vid) {
  67. /** @var \Drupal\heartbeat8\HeartbeatInterface $revision */
  68. $revision = $heartbeat_storage->loadRevision($vid);
  69. // Only show revisions that are affected by the language that is being
  70. // displayed.
  71. if ($revision->hasTranslation($langcode) && $revision->getTranslation($langcode)->isRevisionTranslationAffected()) {
  72. $username = [
  73. '#theme' => 'username',
  74. '#account' => $revision->getRevisionUser(),
  75. ];
  76. // Use revision link to link to revisions that are not active.
  77. $date = \Drupal::service('date.formatter')->format($revision->revision_timestamp->value, 'short');
  78. if ($vid != $heartbeat->getRevisionId()) {
  79. $link = $this->l($date, new Url('entity.heartbeat.revision', ['heartbeat' => $heartbeat->id(), 'heartbeat_revision' => $vid]));
  80. }
  81. else {
  82. $link = $heartbeat->link($date);
  83. }
  84. $row = [];
  85. $column = [
  86. 'data' => [
  87. '#type' => 'inline_template',
  88. '#template' => '{% trans %}{{ date }} by {{ username }}{% endtrans %}{% if message %}<p class="revision-log">{{ message }}</p>{% endif %}',
  89. '#context' => [
  90. 'date' => $link,
  91. 'username' => \Drupal::service('renderer')->renderPlain($username),
  92. 'message' => ['#markup' => $revision->revision_log_message->value, '#allowed_tags' => Xss::getHtmlTagList()],
  93. ],
  94. ],
  95. ];
  96. $row[] = $column;
  97. if ($latest_revision) {
  98. $row[] = [
  99. 'data' => [
  100. '#prefix' => '<em>',
  101. '#markup' => $this->t('Current revision'),
  102. '#suffix' => '</em>',
  103. ],
  104. ];
  105. foreach ($row as &$current) {
  106. $current['class'] = ['revision-current'];
  107. }
  108. $latest_revision = FALSE;
  109. }
  110. else {
  111. $links = [];
  112. if ($revert_permission) {
  113. $links['revert'] = [
  114. 'title' => $this->t('Revert'),
  115. 'url' => $has_translations ?
  116. Url::fromRoute('entity.heartbeat.translation_revert', ['heartbeat' => $heartbeat->id(), 'heartbeat_revision' => $vid, 'langcode' => $langcode]) :
  117. Url::fromRoute('entity.heartbeat.revision_revert', ['heartbeat' => $heartbeat->id(), 'heartbeat_revision' => $vid]),
  118. ];
  119. }
  120. if ($delete_permission) {
  121. $links['delete'] = [
  122. 'title' => $this->t('Delete'),
  123. 'url' => Url::fromRoute('entity.heartbeat.revision_delete', ['heartbeat' => $heartbeat->id(), 'heartbeat_revision' => $vid]),
  124. ];
  125. }
  126. $row[] = [
  127. 'data' => [
  128. '#type' => 'operations',
  129. '#links' => $links,
  130. ],
  131. ];
  132. }
  133. $rows[] = $row;
  134. }
  135. }
  136. $build['heartbeat_revisions_table'] = array(
  137. '#theme' => 'table',
  138. '#rows' => $rows,
  139. '#header' => $header,
  140. );
  141. return $build;
  142. }
  143. }