heartbeat.module 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. $arguments = json_decode($heartbeatTypeEntity->getArguments());
  79. foreach ($arguments as $key => $argument) {
  80. $variables[$key] = $argument;
  81. }
  82. $preparsedMessageString = strtr($heartbeatTypeEntity->getMessage(), $variables);
  83. $entities = array(
  84. 'node' => $entity,
  85. 'user' => $user,
  86. );
  87. if (array_key_exists($heartbeatTypeEntity->getMainEntity(), $heartbeatTypeService->getEntityBundles($entity->getEntityType()))) {
  88. $stophere = null;
  89. }
  90. $heartbeatMessage = Heartbeat::buildMessage($tokenService, $preparsedMessageString, $entities, $entity->getEntityTypeId(), $media);
  91. // $translatedMessage = t($messageTemplate);
  92. $heartbeatActivity = Heartbeat::create([
  93. 'type' => $heartbeatTypeEntity->id(),
  94. 'uid' => $user->id(),
  95. 'nid' => $entity->id(),
  96. 'name' => 'Dev Test',
  97. ]);
  98. $heartbeatActivity->setMessage($heartbeatMessage);
  99. $heartbeatActivity->save();
  100. }
  101. }
  102. break;
  103. case $entity instanceof \Drupal\user\Entity\User:
  104. // $userService = \Drupal\User\Entity\user
  105. break;
  106. case $entity instanceof \Drupal\flag\Entity\Flag:
  107. break;
  108. }
  109. }
  110. /**
  111. * Implements hook_ajax_render_alter().
  112. */
  113. function heartbeat_ajax_render_alter(array &$data)
  114. {
  115. }
  116. //TODO Add heartbeat language to Javascript
  117. //TODO Determine necessity of polling
  118. //Add