Browse Source

Fixing issue which prevented Friend link from rebuilding

logicp 7 years ago
parent
commit
4e7dd47573
2 changed files with 26 additions and 28 deletions
  1. 9 8
      src/Entity/Heartbeat.php
  2. 17 20
      src/EventSubscriber/HeartbeatEventSubscriber.php

+ 9 - 8
src/Entity/Heartbeat.php

@@ -410,14 +410,14 @@ class Heartbeat extends RevisionableContentEntityBase implements HeartbeatInterf
    */
   public static function buildMessage(\Drupal\token\Token $tokenService, $preparsedMessage, $entities = NULL, $entityType, $mediaData = NULL) {
     $options = null;
-    if ($entityType == 'flag') {
-//      $options = [
-//        'callback' => '\Drupal\heartbeat\Entity\Heartbeat::handleMultipleEntities',
-//      ];
+    if ($entityType === 'flag') {
+
       $returnMessage = self::handleMultipleEntities($tokenService, $preparsedMessage, $entities);
+//      $returnMessage = "jigga";
       return strlen($returnMessage) > 0 ? $returnMessage : "Error creating message";
 
     }
+
     $parsedMessage = $tokenService->replace($preparsedMessage . '<a href="/node/[node:nid]">', $entities);
     /** @noinspection NestedTernaryOperatorInspection */
     $message = $parsedMessage;
@@ -449,7 +449,6 @@ class Heartbeat extends RevisionableContentEntityBase implements HeartbeatInterf
     $tokens = $tokenService->scan($message);
 
     foreach($tokens as $key => $token) {
-      echo 'jigga';
       foreach ($token as $type) {
         if (substr_count($message, $type) > 1) {
           foreach ($entities as $entityKey => $entityValue) {
@@ -472,22 +471,24 @@ class Heartbeat extends RevisionableContentEntityBase implements HeartbeatInterf
                   $rebuiltMessage = $replacements[0] . $messageArray[1] . $replacements[1];
                   return $rebuiltMessage;
                 }
-
               }
             }
           }
         }
       }
-
     }
+    return null;
   }
 
   public static function getWordRepeats($phrase) {
     $counts = array();
       $words = explode(' ', $phrase);
       foreach ($words as $word) {
+        if (!array_key_exists($word, $counts)) {
+          $counts[$word] = 0;
+        }
         $word = preg_replace("#[^a-zA-Z\-]#", "", $word);
-        $counts[$word] += 1;
+        ++$counts[$word];
       }
     return $counts;
   }

+ 17 - 20
src/EventSubscriber/HeartbeatEventSubscriber.php

@@ -72,18 +72,17 @@ class HeartbeatEventSubscriber implements EventSubscriberInterface {
    * dispatched.
    *
    * @param GetResponseEvent $event
-   * @return GetResponseEvent|Event
    * @throws \Drupal\Core\Entity\EntityStorageException
    */
   public function flag_entity_flagged(Event $event) {
     $friendStatus = NOT_FRIEND;
     $flagging = $event->getFlagging();
+
     if ($flagging->getFlagId() === 'friendship') {
       $entity = $this->flagService->getFlagById($flagging->getFlagId());
 
       $user = $flagging->getOwner();
 
-
       if ($entity->id() && $user->isAuthenticated()) {
 
         $heartbeatTypeService = \Drupal::service('heartbeat.heartbeattype');
@@ -108,14 +107,6 @@ class HeartbeatEventSubscriber implements EventSubscriberInterface {
 
             $friendStatus = $friendStatus == FRIEND ? FRIEND : PENDING;
 
-            if ($friendStatus === FRIEND) {
-              drupal_set_message($user->getUsername() . ' is now friends with ' . $user2->getUsername());
-            } else if ($friendStatus === PENDING) {
-              drupal_set_message($user->getUsername() . ' has requested friendship with ' . $user2->getUsername());
-            } else {
-              drupal_set_message($user->getUsername() . ' is unable to request friendship with ' . $user2->getUsername());
-            }
-
             foreach ($arguments as $key => $argument) {
               $variables[$key] = $argument;
             }
@@ -131,22 +122,27 @@ class HeartbeatEventSubscriber implements EventSubscriberInterface {
 
             $heartbeatMessage = Heartbeat::buildMessage($tokenService, $preparsedMessageString, $entities, $entity->getEntityTypeId(), null);
 
-            $heartbeatActivity = Heartbeat::create([
-              'type' => $heartbeatTypeEntity->id(),
-              'uid' => $user->id(),
-              'nid' => $entity->id(),
-              'name' => 'Dev Test',
-            ]);
+//            $heartbeatActivity = Heartbeat::create([
+//              'type' => $heartbeatTypeEntity->id(),
+//              'uid' => $user->id(),
+//              'nid' => $entity->id(),
+//              'name' => 'Dev Test',
+//            ]);
 
-            $heartbeatActivity->setMessage($heartbeatMessage);
-            $heartbeatActivity->save();
+//            $heartbeatActivity->setMessage($heartbeatMessage);
+//            $heartbeatActivity->save();
           }
         }
       }
     }
 
-    drupal_set_message('Event flag.entity_flagged thrown by Subscriber in module heartbeat.', 'status', TRUE);
-    return $event;
+//    if ($friendStatus === FRIEND) {
+//      drupal_set_message($user->getUsername() . ' is now friends with ' . $user2->getUsername());
+//    } else if ($friendStatus === PENDING) {
+//      drupal_set_message($user->getUsername() . ' has requested friendship with ' . $user2->getUsername());
+//    } else {
+//      drupal_set_message($user->getUsername() . ' is unable to request friendship with ' . $user2->getUsername());
+//    }
   }
 
   /**
@@ -156,6 +152,7 @@ class HeartbeatEventSubscriber implements EventSubscriberInterface {
    * @param GetResponseEvent $event
    */
   public function flag_entity_unflagged(Event $event) {
+    $nothing = null;
     drupal_set_message('Event flag.entity_unflagged thrown by Subscriber in module heartbeat.', 'status', TRUE);
   }