heartbeat.module 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. return $theme;
  44. }
  45. /**
  46. * Implements hook_theme_suggestions_HOOK().
  47. */
  48. function heartbeat_theme_suggestions_heartbeat(array $variables) {
  49. $suggestions = array();
  50. $entity = $variables['elements']['#heartbeat'];
  51. $sanitized_view_mode = strtr($variables['elements']['#view_mode'], '.', '_');
  52. $suggestions[] = 'heartbeat__' . $sanitized_view_mode;
  53. $suggestions[] = 'heartbeat__' . $entity->bundle();
  54. $suggestions[] = 'heartbeat__' . $entity->bundle() . '__' . $sanitized_view_mode;
  55. $suggestions[] = 'heartbeat__' . $entity->id();
  56. $suggestions[] = 'heartbeat__' . $entity->id() . '__' . $sanitized_view_mode;
  57. return $suggestions;
  58. }
  59. /**
  60. * Implements hook_entity_insert().
  61. */
  62. function heartbeat_entity_insert(EntityInterface $entity) {
  63. if ($entity instanceof \Drupal\node\Entity\Node) {
  64. $nid = $entity->id();
  65. $user = $entity->getOwner();
  66. $type = $entity->getEntityTypeId();
  67. $media = HeartBeat::mediaFieldTypes($entity->getFields());
  68. /** @noinspection NestedTernaryOperatorInspection */
  69. /** @noinspection ArrayCastingEquivalentInspection */
  70. // $variables = array(
  71. // '!nid' => $nid,
  72. // '!entityType' => $type,
  73. // '!uid' => $user->id(),
  74. // '!user' => $user->getAccountName(),
  75. // 'media' => $media,
  76. // );
  77. if ($entity->id() && $user->isAuthenticated()) {
  78. $heartbeatTypeService = \Drupal::service('heartbeat.heartbeattype');
  79. $tokenService = \Drupal::service('token');
  80. foreach ($heartbeatTypeService->getTypes() as $type) {
  81. $heartbeatTypeEntity = $heartbeatTypeService->load($type);
  82. $arguments = json_decode($heartbeatTypeEntity->getArguments());
  83. foreach ($arguments as $key => $argument) {
  84. $variables[$key] = $argument;
  85. }
  86. $preparsedMessageString = strtr($heartbeatTypeEntity->getMessage(), $variables);
  87. $entities = array(
  88. 'node' => $entity,
  89. 'user' => $user,
  90. );
  91. $heartbeatMessage = Heartbeat::buildMessage($tokenService, $preparsedMessageString, $entities, $media);
  92. // $translatedMessage = t($messageTemplate);
  93. $heartbeatActivity = Heartbeat::create([
  94. 'type' => $heartbeatTypeEntity->id(),
  95. 'uid' => $user->id(),
  96. 'nid' => $entity->id(),
  97. 'name' => 'Dev Test',
  98. ]);
  99. $heartbeatActivity->setMessage($heartbeatMessage);
  100. $heartbeatActivity->save();
  101. }
  102. }
  103. }
  104. }
  105. function heartbeat_node_presave(NodeInterface $node) {
  106. $uid = $node->getOwnerId();
  107. $stuffTOdo = null;
  108. }
  109. //TODO Add heartbeat language to Javascript
  110. //TODO Determine necessity of polling
  111. //Add