HeartbeatStreamController.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. <?php
  2. namespace Drupal\heartbeat\Controller;
  3. use Drupal\Component\Utility\Xss;
  4. use Drupal\Core\Controller\ControllerBase;
  5. use Drupal\Core\Entity\Query\QueryFactory;
  6. use Drupal\Core\Entity\EntityTypeManager;
  7. use Drupal\heartbeat\HeartbeatService;
  8. use Drupal\heartbeat\HeartbeatStreamServices;
  9. use Symfony\Component\DependencyInjection\ContainerInterface;
  10. use Drupal\Core\Url;
  11. use Drupal\heartbeat\Entity\HeartbeatStreamInterface;
  12. /**
  13. * Class HeartbeatStreamController.
  14. *
  15. * Returns responses for Heartbeat stream routes.
  16. *
  17. * @package Drupal\heartbeat\Controller
  18. */
  19. class HeartbeatStreamController extends ControllerBase {
  20. /**
  21. * Drupal\Core\Entity\Query\QueryFactory definition.
  22. *
  23. * @var \Drupal\Core\Entity\Query\QueryFactory
  24. */
  25. protected $entityQuery;
  26. /**
  27. * Drupal\Core\Entity\EntityTypeManager definition.
  28. *
  29. * @var \Drupal\Core\Entity\EntityTypeManager
  30. */
  31. protected $entityTypeManager;
  32. /**
  33. * \Drupal\heartbeat\HeartbeatStreamServices definition.
  34. *
  35. * @var \Drupal\heartbeat\HeartbeatStreamServices
  36. */
  37. protected $heartbeatStreamService;
  38. /**
  39. * \Drupal\heartbeat\HeartbeatService definition.
  40. *
  41. * @var \Drupal\heartbeat\HeartbeatService
  42. */
  43. protected $heartbeatService;
  44. /**
  45. * {@inheritdoc}
  46. */
  47. public function __construct(QueryFactory $entity_query, EntityTypeManager $entity_type_manager, HeartbeatStreamServices $heartbeatStreamService, HeartbeatService $heartbeatService) {
  48. $this->entityQuery = $entity_query;
  49. $this->entityTypeManager = $entity_type_manager;
  50. $this->heartbeatStreamService = $heartbeatStreamService;
  51. $this->heartbeatService = $heartbeatService;
  52. }
  53. /**
  54. * {@inheritdoc}
  55. */
  56. public static function create(ContainerInterface $container) {
  57. return new static(
  58. $container->get('entity.query'),
  59. $container->get('entity_type.manager'),
  60. $container->get('heartbeatstream'),
  61. $container->get('heartbeat')
  62. );
  63. }
  64. /**
  65. * Getroutes.
  66. *
  67. * @return string
  68. * Return Hello string.
  69. */
  70. public function createRoute($heartbeatStreamId) {
  71. $messages = array();
  72. $heartbeatTypes = array();
  73. $types = $this->heartbeatStreamService->getTypesById($heartbeatStreamId);
  74. foreach ($types as $type) {
  75. if ($type != null && strlen($type->getValue()['target_id']) > 1) {
  76. $heartbeatTypes[] = $type->getValue()['target_id'];
  77. }
  78. }
  79. $heartbeats = $this->heartbeatService->loadByTypes($heartbeatTypes);
  80. foreach($heartbeats as $heartbeat) {
  81. // $messages[] = $heartbeat->getMessage()->getValue()[0]['value'];
  82. $entity = $this->entityTypeManager->getStorage('heartbeat')->load($heartbeat->id());
  83. $view_builder = $this->entityTypeManager->getViewBuilder('heartbeat');
  84. $pre_render = $view_builder->view($entity);
  85. $render_output = render($pre_render);
  86. $messages[] = $pre_render;
  87. }
  88. return [
  89. '#theme' => 'heartbeat_stream',
  90. '#messages' => $messages,
  91. '#attached' => array('library' => 'heartbeat/heartbeat')
  92. ];
  93. }
  94. /**
  95. * Displays a Heartbeat stream revision.
  96. *
  97. * @param int $heartbeat_stream_revision
  98. * The Heartbeat stream revision ID.
  99. *
  100. * @return array
  101. * An array suitable for drupal_render().
  102. */
  103. public function revisionShow($heartbeat_stream_revision) {
  104. $heartbeat_stream = $this->entityManager()->getStorage('heartbeat_stream')->loadRevision($heartbeat_stream_revision);
  105. $view_builder = $this->entityManager()->getViewBuilder('heartbeat_stream');
  106. return $view_builder->view($heartbeat_stream);
  107. }
  108. /**
  109. * Page title callback for a Heartbeat stream revision.
  110. *
  111. * @param int $heartbeat_stream_revision
  112. * The Heartbeat stream revision ID.
  113. *
  114. * @return string
  115. * The page title.
  116. */
  117. public function revisionPageTitle($heartbeat_stream_revision) {
  118. $heartbeat_stream = $this->entityManager()->getStorage('heartbeat_stream')->loadRevision($heartbeat_stream_revision);
  119. return $this->t('Revision of %title from %date', array('%title' => $heartbeat_stream->label(), '%date' => format_date($heartbeat_stream->getRevisionCreationTime())));
  120. }
  121. /**
  122. * Generates an overview table of older revisions of a Heartbeat stream .
  123. *
  124. * @param \Drupal\heartbeat\Entity\HeartbeatStreamInterface $heartbeat_stream
  125. * A Heartbeat stream object.
  126. *
  127. * @return array
  128. * An array as expected by drupal_render().
  129. */
  130. public function revisionOverview(HeartbeatStreamInterface $heartbeat_stream) {
  131. $account = $this->currentUser();
  132. $langcode = $heartbeat_stream->language()->getId();
  133. $langname = $heartbeat_stream->language()->getName();
  134. $languages = $heartbeat_stream->getTranslationLanguages();
  135. $has_translations = (count($languages) > 1);
  136. $heartbeat_stream_storage = $this->entityManager()->getStorage('heartbeat_stream');
  137. $build['#title'] = $has_translations ? $this->t('@langname revisions for %title', ['@langname' => $langname, '%title' => $heartbeat_stream->label()]) : $this->t('Revisions for %title', ['%title' => $heartbeat_stream->label()]);
  138. $header = array($this->t('Revision'), $this->t('Operations'));
  139. $revert_permission = (($account->hasPermission("revert all heartbeat stream revisions") || $account->hasPermission('administer heartbeat stream entities')));
  140. $delete_permission = (($account->hasPermission("delete all heartbeat stream revisions") || $account->hasPermission('administer heartbeat stream entities')));
  141. $rows = array();
  142. $vids = $heartbeat_stream_storage->revisionIds($heartbeat_stream);
  143. $latest_revision = TRUE;
  144. foreach (array_reverse($vids) as $vid) {
  145. /** @var \Drupal\heartbeat\HeartbeatStreamInterface $revision */
  146. $revision = $heartbeat_stream_storage->loadRevision($vid);
  147. // Only show revisions that are affected by the language that is being
  148. // displayed.
  149. if ($revision->hasTranslation($langcode) && $revision->getTranslation($langcode)->isRevisionTranslationAffected()) {
  150. $username = [
  151. '#theme' => 'username',
  152. '#account' => $revision->getRevisionUser(),
  153. ];
  154. // Use revision link to link to revisions that are not active.
  155. $date = \Drupal::service('date.formatter')->format($revision->revision_timestamp->value, 'short');
  156. if ($vid != $heartbeat_stream->getRevisionId()) {
  157. $link = $this->l($date, new Url('entity.heartbeat_stream.revision', ['heartbeat_stream' => $heartbeat_stream->id(), 'heartbeat_stream_revision' => $vid]));
  158. }
  159. else {
  160. $link = $heartbeat_stream->link($date);
  161. }
  162. $row = [];
  163. $column = [
  164. 'data' => [
  165. '#type' => 'inline_template',
  166. '#template' => '{% trans %}{{ date }} by {{ username }}{% endtrans %}{% if message %}<p class="revision-log">{{ message }}</p>{% endif %}',
  167. '#context' => [
  168. 'date' => $link,
  169. 'username' => \Drupal::service('renderer')->renderPlain($username),
  170. 'message' => ['#markup' => $revision->revision_log_message->value, '#allowed_tags' => Xss::getHtmlTagList()],
  171. ],
  172. ],
  173. ];
  174. $row[] = $column;
  175. if ($latest_revision) {
  176. $row[] = [
  177. 'data' => [
  178. '#prefix' => '<em>',
  179. '#markup' => $this->t('Current revision'),
  180. '#suffix' => '</em>',
  181. ],
  182. ];
  183. foreach ($row as &$current) {
  184. $current['class'] = ['revision-current'];
  185. }
  186. $latest_revision = FALSE;
  187. }
  188. else {
  189. $links = [];
  190. if ($revert_permission) {
  191. $links['revert'] = [
  192. 'title' => $this->t('Revert'),
  193. 'url' => Url::fromRoute('entity.heartbeat_stream.revision_revert', ['heartbeat_stream' => $heartbeat_stream->id(), 'heartbeat_stream_revision' => $vid]),
  194. ];
  195. }
  196. if ($delete_permission) {
  197. $links['delete'] = [
  198. 'title' => $this->t('Delete'),
  199. 'url' => Url::fromRoute('entity.heartbeat_stream.revision_delete', ['heartbeat_stream' => $heartbeat_stream->id(), 'heartbeat_stream_revision' => $vid]),
  200. ];
  201. }
  202. $row[] = [
  203. 'data' => [
  204. '#type' => 'operations',
  205. '#links' => $links,
  206. ],
  207. ];
  208. }
  209. $rows[] = $row;
  210. }
  211. }
  212. $build['heartbeat_stream_revisions_table'] = array(
  213. '#theme' => 'table',
  214. '#rows' => $rows,
  215. '#header' => $header,
  216. );
  217. return $build;
  218. }
  219. }