heartbeat.module 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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. $bundleSaved = false;
  70. switch (true) {
  71. case $entity instanceof \Drupal\node\Entity\Node:
  72. $user = $entity->getOwner();
  73. $media = HeartBeat::mediaFieldTypes($entity->getFields());
  74. if ($entity->id() && $user->isAuthenticated()) {
  75. $heartbeatTypeService = \Drupal::service('heartbeat.heartbeattype');
  76. $tokenService = \Drupal::service('token');
  77. foreach ($heartbeatTypeService->getTypes() as $type) {
  78. $heartbeatTypeEntity = $heartbeatTypeService->load($type);
  79. if ($heartbeatTypeEntity->getMainEntity() == 'node') {
  80. $arguments = json_decode($heartbeatTypeEntity->getArguments());
  81. foreach ($arguments as $key => $argument) {
  82. $variables[$key] = $argument;
  83. }
  84. $preparsedMessageString = strtr($heartbeatTypeEntity->getMessage(), $variables);
  85. $entities = array(
  86. 'node' => $entity,
  87. 'user' => $user,
  88. );
  89. if ($entity->bundle() === $heartbeatTypeEntity->getBundle()
  90. &&
  91. array_key_exists($heartbeatTypeEntity->getBundle(), $heartbeatTypeService->getEntityBundles($entity->getEntityType()))
  92. ) {
  93. $heartbeatMessage = Heartbeat::buildMessage($tokenService, $preparsedMessageString, $entities, $entity->getEntityTypeId(), $media);
  94. $heartbeatActivity = Heartbeat::create([
  95. 'type' => $heartbeatTypeEntity->id(),
  96. 'uid' => $user->id(),
  97. 'nid' => $entity->id(),
  98. 'name' => 'Dev Test',
  99. ]);
  100. $heartbeatActivity->setMessage($heartbeatMessage);
  101. if ($heartbeatActivity->save()) {
  102. $bundleSaved = true;
  103. }
  104. continue;
  105. } else if ($heartbeatTypeEntity->getBundle() === null || trim($heartbeatTypeEntity->getBundle()) === '') {
  106. $heartbeatMessage = Heartbeat::buildMessage($tokenService, $preparsedMessageString, $entities, $entity->getEntityTypeId(), $media);
  107. // $translatedMessage = t($messageTemplate);
  108. $heartbeatActivity = Heartbeat::create([
  109. 'type' => $heartbeatTypeEntity->id(),
  110. 'uid' => $user->id(),
  111. 'nid' => $entity->id(),
  112. 'name' => 'Dev Test',
  113. 'status' => !$bundleSaved ? 1 : 0,
  114. ]);
  115. $heartbeatActivity->setMessage($heartbeatMessage);
  116. $heartbeatActivity->save();
  117. } else {
  118. continue;
  119. }
  120. }
  121. }
  122. }
  123. break;
  124. case $entity instanceof \Drupal\user\Entity\User:
  125. // $userService = \Drupal\User\Entity\user
  126. break;
  127. case $entity instanceof \Drupal\flag\Entity\Flag:
  128. break;
  129. case $entity instanceof \Drupal\statusmessage\Entity\Status:
  130. $user = \Drupal\user\Entity\User::load($entity->get('uid')->getValue()[0]['target_id']);
  131. $heartbeatTypeService = \Drupal::service('heartbeat.heartbeattype');
  132. $tokenService = \Drupal::service('token');
  133. heartbeat_handle_entity($entity, $tokenService, $heartbeatTypeService, $user, 'status');
  134. break;
  135. }
  136. }
  137. function heartbeat_handle_entity($entity, $tokenService, $heartbeatTypeService, $user, $mainEntity) {
  138. $bundleSaved = false;
  139. if ($entity->id() && $user->isAuthenticated()) {
  140. $media = HeartBeat::mediaFieldTypes($entity->getFields());
  141. foreach ($heartbeatTypeService->getTypes() as $type) {
  142. $heartbeatTypeEntity = $heartbeatTypeService->load($type);
  143. if ($heartbeatTypeEntity->getMainEntity() == $mainEntity) {
  144. $arguments = json_decode($heartbeatTypeEntity->getArguments());
  145. foreach ($arguments as $key => $argument) {
  146. $variables[$key] = $argument;
  147. }
  148. $preparsedMessageString = strtr($heartbeatTypeEntity->getMessage(), $variables);
  149. $entities = array(
  150. $mainEntity => $entity,
  151. 'user' => $user,
  152. );
  153. if ($entity->bundle() === $heartbeatTypeEntity->getBundle()
  154. &&
  155. array_key_exists($heartbeatTypeEntity->getBundle(), $heartbeatTypeService->getEntityBundles($entity->getEntityType()))
  156. ) {
  157. $heartbeatMessage = Heartbeat::buildMessage($tokenService, $preparsedMessageString, $entities, $entity->getEntityTypeId(), $media);
  158. $heartbeatActivity = Heartbeat::create([
  159. 'type' => $heartbeatTypeEntity->id(),
  160. 'uid' => $user->id(),
  161. 'nid' => $entity->id(),
  162. 'name' => 'Dev Test',
  163. ]);
  164. $heartbeatActivity->setMessage($heartbeatMessage);
  165. if ($heartbeatActivity->save()) {
  166. $bundleSaved = true;
  167. }
  168. continue;
  169. } else if ($heartbeatTypeEntity->getBundle() === null || trim($heartbeatTypeEntity->getBundle()) === '') {
  170. $heartbeatMessage = Heartbeat::buildMessage($tokenService, $preparsedMessageString, $entities, $entity->getEntityTypeId(), $media);
  171. // $translatedMessage = t($messageTemplate);
  172. $heartbeatActivity = Heartbeat::create([
  173. 'type' => $heartbeatTypeEntity->id(),
  174. 'uid' => $user->id(),
  175. 'nid' => $entity->id(),
  176. 'name' => 'Dev Test',
  177. 'status' => !$bundleSaved ? 1 : 0,
  178. ]);
  179. $heartbeatActivity->setMessage($heartbeatMessage);
  180. $heartbeatActivity->save();
  181. } else {
  182. continue;
  183. }
  184. }
  185. }
  186. }
  187. }
  188. //public function saveHeartbeatMessage($entity, $tokenService, $preparsedMessageString, $entities, $entityTypeId, $media) {
  189. // $heartbeatMessage = Heartbeat::buildMessage($tokenService, $preparsedMessageString, $entities, $entity->getEntityTypeId(), $media);
  190. //
  191. //
  192. // $heartbeatActivity = Heartbeat::create([
  193. // 'type' => $heartbeatTypeEntity->id(),
  194. // 'uid' => $user->id(),
  195. // 'nid' => $entity->id(),
  196. // 'name' => 'Dev Test',
  197. // ]);
  198. //
  199. // $heartbeatActivity->setMessage($heartbeatMessage);
  200. // $heartbeatActivity->save();
  201. //}
  202. /**
  203. * Implements hook_ajax_render_alter().
  204. */
  205. function heartbeat_ajax_render_alter(array &$data) {
  206. }
  207. //TODO Add heartbeat language to Javascript
  208. //TODO Determine necessity of polling
  209. //Add
  210. //TODO implement polling in JS using Drupal Settings
  211. //$variables['#attached']['drupalSettings']['heartbeatData'] = $mydata;
  212. //^ will become available in JS as:
  213. //settings.heartbeatData, data