heartbeat.module 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. }
  78. if ($nid) {
  79. $heartbeatTypeService = \Drupal::service('heartbeat.heartbeattype');
  80. $tokenService= \Drupal::service('token');
  81. foreach ($heartbeatTypeService->getTypes() as $type) {
  82. $heartbeatTypeEntity = $heartbeatTypeService->load($type);
  83. $arguments = json_decode($heartbeatTypeEntity->getArguments());
  84. $argKeys = array_keys($arguments);
  85. foreach ($arguments as $key => $argument) {
  86. $variables[$key] = $argument;
  87. }
  88. $preparsedMessageString = strtr($heartbeatTypeEntity->getMessage(), $variables);
  89. $replacedMessageString = $tokenService->replace($preparsedMessageString, array('node' => $entity, 'user' => $user));
  90. $messageTemplate = Heartbeat::buildMessage($heartbeatTypeEntity, $media);
  91. // $translatedMessage = t($messageTemplate);
  92. $heartbeatMarkup = Heartbeat::parseMessage($messageTemplate, $variables);
  93. // $heartbeatActivity = new Heartbeat();
  94. // $heartbeatActivity->setName();
  95. // $heartbeatActivity->save();
  96. }
  97. }
  98. }
  99. function heartbeat_node_presave(NodeInterface $node) {
  100. $uid = $node->getOwnerId();
  101. $stuffTOdo = null;
  102. }
  103. //TODO Add heartbeat language to Javascript
  104. //TODO Determine necessity of polling
  105. //Add