فهرست منبع

Heartbeat now captures Entity and Bundle dynamically!
new stream method added to test dynamically derived superherostream

logicp 7 سال پیش
والد
کامیت
9d1758ae1b
3فایلهای تغییر یافته به همراه107 افزوده شده و 2 حذف شده
  1. 33 2
      heartbeat.module
  2. 16 0
      heartbeat.routing.yml
  3. 58 0
      src/Controller/StreamTestController.php

+ 33 - 2
heartbeat.module

@@ -111,9 +111,25 @@ function heartbeat_entity_insert(EntityInterface $entity) {
               'node' => $entity,
               'user' => $user,
             );
-
-            if (array_key_exists($heartbeatTypeEntity->getMainEntity(), $heartbeatTypeService->getEntityBundles($entity->getEntityType()))) {
+            $bundle = $entity->bundle();
+            $hbTypeBundle = $heartbeatTypeEntity->getBundle();
+            if (array_key_exists($heartbeatTypeEntity->getBundle(), $heartbeatTypeService->getEntityBundles($entity->getEntityType()))
+            &&
+            $entity->bundle() === $heartbeatTypeEntity->getBundle()) {
     //TODO handle logic for bundle-specific Heartbeat types based on the Node entity
+              $heartbeatMessage = Heartbeat::buildMessage($tokenService, $preparsedMessageString, $entities, $entity->getEntityTypeId(), $media);
+
+
+              $heartbeatActivity = Heartbeat::create([
+                'type' => $heartbeatTypeEntity->id(),
+                'uid' => $user->id(),
+                'nid' => $entity->id(),
+                'name' => 'Dev Test',
+              ]);
+
+              $heartbeatActivity->setMessage($heartbeatMessage);
+              $heartbeatActivity->save();
+              break;
             }
 
             $heartbeatMessage = Heartbeat::buildMessage($tokenService, $preparsedMessageString, $entities, $entity->getEntityTypeId(), $media);
@@ -145,6 +161,21 @@ function heartbeat_entity_insert(EntityInterface $entity) {
 
 }
 
+//public function saveHeartbeatMessage($entity, $tokenService, $preparsedMessageString, $entities, $entityTypeId, $media) {
+//  $heartbeatMessage = Heartbeat::buildMessage($tokenService, $preparsedMessageString, $entities, $entity->getEntityTypeId(), $media);
+//
+//
+//  $heartbeatActivity = Heartbeat::create([
+//    'type' => $heartbeatTypeEntity->id(),
+//    'uid' => $user->id(),
+//    'nid' => $entity->id(),
+//    'name' => 'Dev Test',
+//  ]);
+//
+//  $heartbeatActivity->setMessage($heartbeatMessage);
+//  $heartbeatActivity->save();
+//}
+
 /**
  * Implements hook_ajax_render_alter().
  */

+ 16 - 0
heartbeat.routing.yml

@@ -31,3 +31,19 @@ heartbeat.stream_test_controller_friendstream:
     _title: 'Friendship'
   requirements:
     _permission: 'access content'
+
+heartbeat.stream_test_controller_tweetstream:
+  path: '/tweetactivity'
+  defaults:
+    _controller: '\Drupal\heartbeat\Controller\StreamTestController::tweetstream'
+    _title: 'Tweetship'
+  requirements:
+    _permission: 'access content'
+
+heartbeat.stream_test_controller_superherostream:
+  path: '/superheroactivity'
+  defaults:
+    _controller: '\Drupal\heartbeat\Controller\StreamTestController::superherostream'
+    _title: 'Superhero Worship'
+  requirements:
+    _permission: 'access content'

+ 58 - 0
src/Controller/StreamTestController.php

@@ -124,4 +124,62 @@ class StreamTestController extends ControllerBase {
     ];
   }
 
+  /**
+   * Stream.
+   *
+   * @return string
+   *   Return Hello string.
+   */
+  public function tweetstream() {
+    $messages = array();
+    $types = $this->heartbeatStreamService->getTypesById(3);
+    foreach ($types as $type) {
+      if ($type != null) {
+
+        $heartbeatType = $type->getValue();
+
+        $heartbeats = $this->heartbeatService->loadByType($heartbeatType);
+
+        foreach($heartbeats as $heartbeat) {
+          $messages[] = $heartbeat->getMessage()->getValue()[0]['value'];
+        }
+      }
+    }
+
+    return [
+      '#theme' => 'heartbeat_stream',
+      '#messages' => array_reverse($messages),
+      '#attached' => array('library' => 'heartbeat/heartbeat')
+    ];
+  }
+
+  /**
+   * Stream.
+   *
+   * @return string
+   *   Return Hello string.
+   */
+  public function superherostream() {
+    $messages = array();
+    $types = $this->heartbeatStreamService->getTypesById(4);
+    foreach ($types as $type) {
+      if ($type != null) {
+
+        $heartbeatType = $type->getValue();
+
+        $heartbeats = $this->heartbeatService->loadByType($heartbeatType);
+
+        foreach($heartbeats as $heartbeat) {
+          $messages[] = $heartbeat->getMessage()->getValue()[0]['value'];
+        }
+      }
+    }
+
+    return [
+      '#theme' => 'heartbeat_stream',
+      '#messages' => array_reverse($messages),
+      '#attached' => array('library' => 'heartbeat/heartbeat')
+    ];
+  }
+
 }