heartbeat.streams.inc 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. use \Drupal\Core\Extension\ModuleHandler;
  3. /**
  4. * Prepares a heartbeat stream
  5. * @param $stream_name the name of the stream
  6. */
  7. function heartbeat_stream($stream_name, $page = 0, $account = NULL) {
  8. static $streams;
  9. try {
  10. if (!isset($streams[$stream_name])) {
  11. //Load configuration object for the stream
  12. $streamConfig = heartbeat_stream_config_load($stream_name);
  13. //Load the stream if access is permitted
  14. if (_heartbeat_stream_has_access($streamConfig)) {
  15. $heartbeatStream = HeartbeatStreamFactory::getStream($streamConfig, $page, $account);
  16. //Add settings that need to be set for each stream
  17. //TODO equivalent drupal_add_js to attach streamconfig messages to the drupalSettings object
  18. /*
  19. * drupal_add_js(array('heartbeatPollNewerMessages' => array($streamConfig->class => $streamConfig->poll_messages)), 'setting');
  20. * drupal_add_js(array('heartbeatPollTypes' => array($streamConfig->class => $streamConfig->poll_messages_type)), 'setting');
  21. *
  22. *
  23. */
  24. //Give other modules a chance to take action when stream is loaded
  25. ModuleHandler::invokeAll('heartbeat_stream_load', $heartbeatStream);
  26. if ($heartbeatStream instanceof HeartbeatStream && !$heartbeatStream->hasErrors()) {
  27. $streams[$stream_name] = $heartbeatStream;
  28. } else {
  29. $streams[$stream_name] = FALSE;
  30. }
  31. } else {
  32. $streams[$stream_name] = FALSE;
  33. }
  34. }
  35. } catch (HeartbeatInvalidStreamException $he) {
  36. $errorMessage = $he->getMessage();
  37. drupal_set_message($errorMessage);
  38. \Drupal::logger('HeartbeatDEBUG')->error('Error message: %errMsg', array(
  39. '%errMsg' => $errorMessage
  40. ));
  41. }
  42. return $streams[$stream_name];
  43. }
  44. /*
  45. * Builds a heartbeat stream in the StreamObject
  46. */
  47. function heartbeat_stream_build(HeartbeatStream &$heartbeatStream) {
  48. //Load messages
  49. $messages = $heartbeatStream->execute();
  50. }
  51. /*
  52. * Builds a stream from Views Results
  53. */
  54. function heartbeat_stream_views_build(HeartbeatStream &$heartbeatStream, $view_mode = NULL) {
  55. if (isset($view_mode)) {
  56. $heartbeatStream->setViewMode($view_mode);
  57. }
  58. $build = $heartbeatStream->render();
  59. return $build;
  60. }
  61. /**
  62. * Load all the stream configuration objects
  63. * @param bool $reset
  64. * Indicates whether the stream data needs to be rebuilt
  65. */
  66. function heartbeat_stream_config_load_all($reset = FALSE) {
  67. $streams = &drupal_static('heartbeat_streams');
  68. if (!$reset && $object = cache_get('heartbeat_streams')) {
  69. $streams = $object->data;
  70. } else {
  71. //TODO fetch streams with alternative to ctools export
  72. }
  73. }