heartbeat.module 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php
  2. /**
  3. * @file
  4. * Contains heartbeat.module.
  5. */
  6. use Drupal\Core\Routing\RouteMatchInterface;
  7. use Drupal\Core\Entity\EntityInterface;
  8. use Drupal\node\NodeInterface;
  9. use Drupal\Core\Session\AccountInterface;
  10. use Drupal\Core\Entity;
  11. use Drupal\heartbeat\Entity\FILE_FIELD;
  12. use Drupal\heartbeat\Entity\Heartbeat;
  13. use Drupal\heartbeat\Entity\HeartbeatType;
  14. /**
  15. * Implements hook_help().
  16. */
  17. function heartbeat_help($route_name, RouteMatchInterface $route_match) {
  18. switch ($route_name) {
  19. // Main module help for the heartbeat module.
  20. case 'help.page.heartbeat':
  21. $output = '';
  22. $output .= '<h3>' . t('About') . '</h3>';
  23. $output .= '<p>' . t('Heartbeat for Drupal 8') . '</p>';
  24. return $output;
  25. default:
  26. }
  27. }
  28. /**
  29. * Implements hook_theme().
  30. */
  31. function heartbeat_theme() {
  32. $theme = [];
  33. $theme['heartbeat'] = array(
  34. 'render element' => 'elements',
  35. 'file' => 'heartbeat.page.inc',
  36. 'template' => 'heartbeat',
  37. );
  38. $theme['heartbeat_content_add_list'] = [
  39. 'render element' => 'content',
  40. 'variables' => ['content' => NULL],
  41. 'file' => 'heartbeat.page.inc',
  42. ];
  43. $theme['heartbeat_stream'] = [
  44. 'variables' => array(
  45. 'messages' => NULL,
  46. 'zilla' => 2,
  47. )
  48. ];
  49. return $theme;
  50. }
  51. /**
  52. * Implements hook_theme_suggestions_HOOK().
  53. */
  54. function heartbeat_theme_suggestions_heartbeat(array $variables) {
  55. $suggestions = array();
  56. $entity = $variables['elements']['#heartbeat'];
  57. $sanitized_view_mode = strtr($variables['elements']['#view_mode'], '.', '_');
  58. $suggestions[] = 'heartbeat__' . $sanitized_view_mode;
  59. $suggestions[] = 'heartbeat__' . $entity->bundle();
  60. $suggestions[] = 'heartbeat__' . $entity->bundle() . '__' . $sanitized_view_mode;
  61. $suggestions[] = 'heartbeat__' . $entity->id();
  62. $suggestions[] = 'heartbeat__' . $entity->id() . '__' . $sanitized_view_mode;
  63. return $suggestions;
  64. }
  65. /**
  66. * Implements hook_entity_insert().
  67. */
  68. function heartbeat_entity_insert(EntityInterface $entity) {
  69. switch (true) {
  70. case $entity instanceof \Drupal\node\Entity\Node:
  71. $user = $entity->getOwner();
  72. $media = HeartBeat::mediaFieldTypes($entity->getFields());
  73. if ($entity->id() && $user->isAuthenticated()) {
  74. $heartbeatTypeService = \Drupal::service('heartbeat.heartbeattype');
  75. $tokenService = \Drupal::service('token');
  76. foreach ($heartbeatTypeService->getTypes() as $type) {
  77. $heartbeatTypeEntity = $heartbeatTypeService->load($type);
  78. if ($heartbeatTypeEntity->getMainEntity() == 'node') {
  79. $arguments = json_decode($heartbeatTypeEntity->getArguments());
  80. foreach ($arguments as $key => $argument) {
  81. $variables[$key] = $argument;
  82. }
  83. $preparsedMessageString = strtr($heartbeatTypeEntity->getMessage(), $variables);
  84. $entities = array(
  85. 'node' => $entity,
  86. 'user' => $user,
  87. );
  88. $bundle = $entity->bundle();
  89. $hbTypeBundle = $heartbeatTypeEntity->getBundle();
  90. if (array_key_exists($heartbeatTypeEntity->getBundle(), $heartbeatTypeService->getEntityBundles($entity->getEntityType()))
  91. &&
  92. $entity->bundle() === $heartbeatTypeEntity->getBundle()) {
  93. //TODO handle logic for bundle-specific Heartbeat types based on the Node entity
  94. $heartbeatMessage = Heartbeat::buildMessage($tokenService, $preparsedMessageString, $entities, $entity->getEntityTypeId(), $media);
  95. $heartbeatActivity = Heartbeat::create([
  96. 'type' => $heartbeatTypeEntity->id(),
  97. 'uid' => $user->id(),
  98. 'nid' => $entity->id(),
  99. 'name' => 'Dev Test',
  100. ]);
  101. $heartbeatActivity->setMessage($heartbeatMessage);
  102. $heartbeatActivity->save();
  103. continue;
  104. } else if ($heartbeatTypeEntity->getBundle() === null || trim($heartbeatTypeEntity->getBundle()) === '') {
  105. $heartbeatMessage = Heartbeat::buildMessage($tokenService, $preparsedMessageString, $entities, $entity->getEntityTypeId(), $media);
  106. // $translatedMessage = t($messageTemplate);
  107. $heartbeatActivity = Heartbeat::create([
  108. 'type' => $heartbeatTypeEntity->id(),
  109. 'uid' => $user->id(),
  110. 'nid' => $entity->id(),
  111. 'name' => 'Dev Test',
  112. ]);
  113. $heartbeatActivity->setMessage($heartbeatMessage);
  114. $heartbeatActivity->save();
  115. } else {
  116. continue;
  117. }
  118. }
  119. }
  120. }
  121. break;
  122. case $entity instanceof \Drupal\user\Entity\User:
  123. // $userService = \Drupal\User\Entity\user
  124. break;
  125. case $entity instanceof \Drupal\flag\Entity\Flag:
  126. break;
  127. }
  128. }
  129. //public function saveHeartbeatMessage($entity, $tokenService, $preparsedMessageString, $entities, $entityTypeId, $media) {
  130. // $heartbeatMessage = Heartbeat::buildMessage($tokenService, $preparsedMessageString, $entities, $entity->getEntityTypeId(), $media);
  131. //
  132. //
  133. // $heartbeatActivity = Heartbeat::create([
  134. // 'type' => $heartbeatTypeEntity->id(),
  135. // 'uid' => $user->id(),
  136. // 'nid' => $entity->id(),
  137. // 'name' => 'Dev Test',
  138. // ]);
  139. //
  140. // $heartbeatActivity->setMessage($heartbeatMessage);
  141. // $heartbeatActivity->save();
  142. //}
  143. /**
  144. * Implements hook_ajax_render_alter().
  145. */
  146. function heartbeat_ajax_render_alter(array &$data)
  147. {
  148. }
  149. //TODO Add heartbeat language to Javascript
  150. //TODO Determine necessity of polling
  151. //Add