HeartbeatController.php 6.7 KB

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