فهرست منبع

entity_insert now handles Status Messages
TODO: Must dynamically handle the addition of anchor tags pointing to iteration of entity

logicp 7 سال پیش
والد
کامیت
ccb5f04089
1فایلهای تغییر یافته به همراه87 افزوده شده و 8 حذف شده
  1. 87 8
      heartbeat.module

+ 87 - 8
heartbeat.module

@@ -111,12 +111,12 @@ function heartbeat_entity_insert(EntityInterface $entity) {
               'node' => $entity,
               'user' => $user,
             );
-            $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
+
+            if ($entity->bundle() === $heartbeatTypeEntity->getBundle()
+              &&
+              array_key_exists($heartbeatTypeEntity->getBundle(), $heartbeatTypeService->getEntityBundles($entity->getEntityType()))
+            ) {
+
               $heartbeatMessage = Heartbeat::buildMessage($tokenService, $preparsedMessageString, $entities, $entity->getEntityTypeId(), $media);
 
 
@@ -159,11 +159,90 @@ function heartbeat_entity_insert(EntityInterface $entity) {
       break;
     case $entity instanceof \Drupal\flag\Entity\Flag:
       break;
+
+    case $entity instanceof \Drupal\statusmessage\Entity\Status:
+
+      $user = \Drupal\user\Entity\User::load($entity->get('uid')->getValue()[0]['target_id']);
+      $heartbeatTypeService = \Drupal::service('heartbeat.heartbeattype');
+      $tokenService = \Drupal::service('token');
+
+      heartbeat_handle_entity($entity, $tokenService, $heartbeatTypeService, $user, 'status');
+
+      break;
+
   }
+}
 
 
+function heartbeat_handle_entity($entity, $tokenService, $heartbeatTypeService, $user, $mainEntity) {
+
+  if ($entity->id() && $user->isAuthenticated()) {
+
+    $media = HeartBeat::mediaFieldTypes($entity->getFields());
+
+    foreach ($heartbeatTypeService->getTypes() as $type) {
+
+      $heartbeatTypeEntity = $heartbeatTypeService->load($type);
+
+      if ($heartbeatTypeEntity->getMainEntity() == $mainEntity) {
+
+        $arguments = json_decode($heartbeatTypeEntity->getArguments());
+
+        foreach ($arguments as $key => $argument) {
+          $variables[$key] = $argument;
+        }
+
+        $preparsedMessageString = strtr($heartbeatTypeEntity->getMessage(), $variables);
+
+        $entities = array(
+          $mainEntity => $entity,
+          'user' => $user,
+        );
+
+        if ($entity->bundle() === $heartbeatTypeEntity->getBundle()
+          &&
+          array_key_exists($heartbeatTypeEntity->getBundle(), $heartbeatTypeService->getEntityBundles($entity->getEntityType()))
+        ) {
+
+          $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();
+          continue;
+        } else if ($heartbeatTypeEntity->getBundle() === null || trim($heartbeatTypeEntity->getBundle()) === '') {
+
+          $heartbeatMessage = Heartbeat::buildMessage($tokenService, $preparsedMessageString, $entities, $entity->getEntityTypeId(), $media);
+
+          //      $translatedMessage = t($messageTemplate);
+
+          $heartbeatActivity = Heartbeat::create([
+            'type' => $heartbeatTypeEntity->id(),
+            'uid' => $user->id(),
+            'nid' => $entity->id(),
+            'name' => 'Dev Test',
+          ]);
+
+          $heartbeatActivity->setMessage($heartbeatMessage);
+          $heartbeatActivity->save();
+
+        } else {
+          continue;
+        }
+
+      }
+    }
+  }
 }
 
+
 //public function saveHeartbeatMessage($entity, $tokenService, $preparsedMessageString, $entities, $entityTypeId, $media) {
 //  $heartbeatMessage = Heartbeat::buildMessage($tokenService, $preparsedMessageString, $entities, $entity->getEntityTypeId(), $media);
 //
@@ -179,11 +258,11 @@ function heartbeat_entity_insert(EntityInterface $entity) {
 //  $heartbeatActivity->save();
 //}
 
+
 /**
  * Implements hook_ajax_render_alter().
  */
-function heartbeat_ajax_render_alter(array &$data)
-{
+function heartbeat_ajax_render_alter(array &$data) {
 
 }
 //TODO Add heartbeat language to Javascript