Browse Source

New ajax method uses lazyloader to build flag markup
old method still available for legacy and/or future solution

logicp 7 years ago
parent
commit
88db2fc6be

+ 4 - 4
css/heartbeat.css

@@ -173,10 +173,10 @@ Temporarily hiding all irrelevant content */
   display: none;
   position: fixed;
   bottom: 48px;
-  right: 24px;
-  min-width: 64px;
-  min-height: 64px;
-  background: url('../images/drupal-loader.gif');
+  left: 62.5%;
+  width: 96px;
+  height: 96px;
+  background: url('../images/drupal-loader.gif') no-repeat;
   background-size: contain;
 }
 

+ 31 - 15
src/Entity/Heartbeat.php

@@ -329,10 +329,10 @@ class Heartbeat extends RevisionableContentEntityBase implements HeartbeatInterf
         'type' => 'string',
         'weight' => -4,
       ))
-      ->setDisplayOptions('form', array(
-        'type' => 'string_textfield',
-        'weight' => -4,
-      ))
+//      ->setDisplayOptions('form', array(
+//        'type' => 'string_textfield',
+//        'weight' => -4,
+//      ))
       ->setDisplayConfigurable('form', TRUE)
       ->setDisplayConfigurable('view', TRUE);
 
@@ -346,6 +346,7 @@ class Heartbeat extends RevisionableContentEntityBase implements HeartbeatInterf
         'type' => 'full_html',
         'weight' => -4,
       ))
+      ->setDisplayConfigurable('form', TRUE)
       ->setDisplayConfigurable('view', TRUE);
 
     $fields['comments'] = BaseFieldDefinition::create('comment')
@@ -437,8 +438,6 @@ class Heartbeat extends RevisionableContentEntityBase implements HeartbeatInterf
    * @return string|false
    *   The heartbeat type label or FALSE if the heartbeat type is not found.
    *
-   * @todo Add this as generic helper method for config entities representing
-   *   entity bundles.
    */
   public function heartbeat_get_type(HeartbeatInterface $heartbeat) {
     $type = HeartbeatType::load($heartbeat->bundle());
@@ -546,7 +545,6 @@ class Heartbeat extends RevisionableContentEntityBase implements HeartbeatInterf
   }
 
   private static function mediaTag($type, $filePath) {
-    //TODO put this into new method
     if ($type == 'image') {
       $type = 'img';
       return '<' . $type . ' src="' . str_replace('public://', '/sites/default/files/', $filePath) . '" class="heartbeat-image" / >';
@@ -652,9 +650,6 @@ class Heartbeat extends RevisionableContentEntityBase implements HeartbeatInterf
       $message .= $replacements;
 
     }
-    if (!strpos($message, '#') || strpos($message, '#') !== 1) {
-      $message = '#' . $message;
-    }
   }
 
 
@@ -848,9 +843,30 @@ class Heartbeat extends RevisionableContentEntityBase implements HeartbeatInterf
     $options['query']['destination'] = 'node';
     $link->getUrl()->setOptions($options);
     $action = $flag->getLinkTypePlugin()->getAsFlagLink($flag, $entity)['#action'];
+//    if ($action) {
     $url = $link->getUrl()->toString();
 
-    return '<div class="flag flag-' . $flagId . '  flag-' . $flagId . '-' . $entity->id() . ' action-' . $action. '"><a href="' . $url . '" class="use-ajax" rel="nofollow"></a></div>';
+    return '<div class="flag flag-' . $flagId . '  flag-' . $flagId . '-' . $entity->id() . ' action-' . $action . '"><a href="' . $url . '" class="use-ajax" rel="nofollow"></a></div>';
+//    } else {
+//      return null;
+//    }
+  }
+
+  public static function flagAjaxBuilder($flagId, $entity, FlagService $flagService) {
+
+    $flag = $flagService->getFlagById($flagId);
+
+    $key = 'flag_' . $flag->id();
+    $data = [
+      '#lazy_builder' => ['flag.link_builder:build', [
+        $entity->getEntityTypeId(),
+        $entity->id(),
+        $flag->id(),
+      ]],
+      '#create_placeholder' => TRUE,
+    ];
+
+    return [$key => $data];
   }
 
   /**
@@ -956,9 +972,8 @@ class Heartbeat extends RevisionableContentEntityBase implements HeartbeatInterf
    * @return int
    *   The uid of the Heartbeat's user.
    */
-  public function getUid()
-  {
-    // TODO: Implement getUid() method.
+  public function getUid() {
+    return $this->get('uid');
   }
 
   /**
@@ -967,10 +982,11 @@ class Heartbeat extends RevisionableContentEntityBase implements HeartbeatInterf
    * @param int uid
    *   The Heartbeat user.
    *
+   * @throws \Drupal\Core\Entity\EntityStorageException
    */
   public function setUid($uid)
   {
-    // TODO: Implement setUid() method.
+    $this->set('uid', $uid)->save();
   }
 
   /**

+ 4 - 26
src/Entity/HeartbeatType.php

@@ -226,26 +226,6 @@ class HeartbeatType extends ConfigEntityBundleBase implements HeartbeatTypeInter
     return $this->variables;
   }
 
-//  /**
-//   * Sets the attachments for this message stream
-//   *
-//   * @param string $attachments
-//   *
-//   */
-//  public function setAttachments($attachments) {
-//    $this->attachments = $attachments;
-//  }
-//
-//  /**
-//   * Gets the attachments of this message stream
-//   *
-//   * @return string
-//   *  The stream's attachments
-//   */
-//  public function getAttachments() {
-//    return $this->attachments;
-//  }
-
   /**
    * @inheritDoc
    */
@@ -259,17 +239,15 @@ class HeartbeatType extends ConfigEntityBundleBase implements HeartbeatTypeInter
   /**
    * @inheritDoc
    */
-  protected function entityManager()
-  {
-    return parent::entityManager(); // TODO: Change the autogenerated stub
+  protected function entityManager(){
+    return parent::entityManager();
   }
 
   /**
    * @inheritDoc
    */
-  protected function entityTypeManager()
-  {
-    return parent::entityTypeManager(); // TODO: Change the autogenerated stub
+  protected function entityTypeManager() {
+    return parent::entityTypeManager();
   }
 
 

+ 10 - 0
src/HeartbeatStreamServices.php

@@ -149,6 +149,16 @@ class HeartbeatStreamServices {
         ->execute());
   }
 
+//  public function createStreamForRecipient($uid) {
+//    return $this->entityTypeManager->getStorage('heartbeat')->loadMultiple(
+//      $this->entityQuery->get(
+//        'heartbeat')
+//      ->condition('status', 1)
+//      ->orConditionGroup()->condition(
+//      )
+//    )
+//  }
+
   public function createStreamByType($type) {
     $stream = $this->entityTypeManager->getStorage(
       'heartbeat_stream')->load(array_values($this->loadStream($type))[0]);

+ 50 - 48
src/Plugin/Block/HeartbeatBlock.php

@@ -120,57 +120,59 @@ class HeartbeatBlock extends BlockBase implements ContainerFactoryPluginInterfac
    */
   public function build() {
 
-    $myConfig = \Drupal::service('config.factory')->getEditable('heartbeat_feed.settings');
-    $friendData = \Drupal::config('heartbeat_friendship.settings')->get('data');
-
-    $feed = $myConfig->get('message');
-    $uids = null;
-    $messages = array();
-
-    $query = Database::getConnection()->select('heartbeat_friendship', 'hf')
-      ->fields('hf',['uid', 'uid_target']);
-    $conditionOr = $query->orConditionGroup()
-      ->condition('hf.uid', \Drupal::currentUser()->id())
-      ->condition('hf.uid_target', \Drupal::currentUser()->id());
-
-    $results = $query->condition($conditionOr)->execute();
-    if ($result = $results->fetchAll()) {
-      $uids = array();
-      foreach ($result as $uid) {
-        $uids[] = $uid->uid_target;
-        $uids[] = $uid->uid;
+    if (\Drupal::currentUser()->id() > 0) {
+      $myConfig = \Drupal::service('config.factory')->getEditable('heartbeat_feed.settings');
+      $friendData = \Drupal::config('heartbeat_friendship.settings')->get('data');
+
+      $feed = $myConfig->get('message');
+      $uids = null;
+      $messages = array();
+
+      $query = Database::getConnection()->select('heartbeat_friendship', 'hf')
+        ->fields('hf', ['uid', 'uid_target']);
+      $conditionOr = $query->orConditionGroup()
+        ->condition('hf.uid', \Drupal::currentUser()->id())
+        ->condition('hf.uid_target', \Drupal::currentUser()->id());
+
+      $results = $query->condition($conditionOr)->execute();
+      if ($result = $results->fetchAll()) {
+        $uids = array();
+        foreach ($result as $uid) {
+          $uids[] = $uid->uid_target;
+          $uids[] = $uid->uid;
+        }
       }
-    }
-    if ($feed !== null && $this->heartbeatStreamServices) {
-    $uids = count($uids) > 1 ? array_unique($uids) : $uids;
-      if (!empty($uids)) {
-        foreach ($this->heartbeatStreamServices->createStreamForUidsByType($uids, $feed) as $heartbeat) {
-          $this->renderMessage($messages, $heartbeat);
+      if ($feed !== null && $this->heartbeatStreamServices) {
+        $uids = count($uids) > 1 ? array_unique($uids) : $uids;
+        if (!empty($uids)) {
+          foreach ($this->heartbeatStreamServices->createStreamForUidsByType($uids, $feed) as $heartbeat) {
+            $this->renderMessage($messages, $heartbeat);
+          }
+        } else {
+          foreach ($this->heartbeatStreamServices->createStreamByType($feed) as $heartbeat) {
+            $this->renderMessage($messages, $heartbeat);
+          }
         }
       } else {
-        foreach ($this->heartbeatStreamServices->createStreamByType($feed) as $heartbeat) {
+        foreach ($this->heartbeatStreamServices->loadAllStreams() as $heartbeat) {
           $this->renderMessage($messages, $heartbeat);
         }
       }
-    } else {
-      foreach ($this->heartbeatStreamServices->loadAllStreams() as $heartbeat) {
-        $this->renderMessage($messages, $heartbeat);
-      }
-    }
-
-    return [
-      '#theme' => 'heartbeat_stream',
-      '#messages' => $messages,
-      '#attached' => array(
-        'library' => 'heartbeat/heartbeat',
-        'drupalSettings' => [
-          'activeFeed' => 'jigga',
-          'friendData' => $friendData,
-        ]
-      ),
-      '#cache' => array('max-age' => 0)
-    ];
 
+      return [
+        '#theme' => 'heartbeat_stream',
+        '#messages' => $messages,
+        '#attached' => array(
+          'library' => 'heartbeat/heartbeat',
+          'drupalSettings' => [
+            'activeFeed' => 'jigga',
+            'friendData' => $friendData,
+          ]
+        ),
+        '#cache' => array('max-age' => 0)
+      ];
+    }
+    return null;
   }
 
     private function renderMessage(array &$messages, $heartbeat) {
@@ -260,7 +262,7 @@ class HeartbeatBlock extends BlockBase implements ContainerFactoryPluginInterfac
               'username' => $subComment->getAuthorName(),
               'owner' => $subCommentOwner,
               'timeAgo' => $subCommentTime,
-              'commentLike' => Heartbeat::flagAjaxMarkup('heartbeat_like_comment', $subComment, $this->flagService)
+              'commentLike' => Heartbeat::flagAjaxBuilder('heartbeat_like_comment', $subComment, $this->flagService)
             ];
 
           }
@@ -286,7 +288,7 @@ class HeartbeatBlock extends BlockBase implements ContainerFactoryPluginInterfac
           'username' => $comment->getAuthorName(),
           'owner' => $commentOwner,
           'timeAgo' => $cTimeago,
-          'commentLike' => Heartbeat::flagAjaxMarkup('heartbeat_like_comment', $comment, $this->flagService),
+          'commentLike' => Heartbeat::flagAjaxBuilder('heartbeat_like_comment', $comment, $this->flagService),
           'reply' => $commentLink,
           'subComments' => $subComments
         ];
@@ -305,8 +307,8 @@ class HeartbeatBlock extends BlockBase implements ContainerFactoryPluginInterfac
         'commentForm' => $form,
         'comments' => array_reverse($comments),
         'commentCount' => $commentCount > 0 ? $commentCount : '',
-        'likeFlag' => Heartbeat::flagAjaxMarkup('heartbeat_like', $heartbeat, $this->flagService),
-        'unlikeFlag' => Heartbeat::flagAjaxMarkup('jihad_flag', $heartbeat, $this->flagService)
+        'likeFlag' => Heartbeat::flagAjaxBuilder('heartbeat_like', $heartbeat, $this->flagService),
+        'unlikeFlag' => Heartbeat::flagAjaxBuilder('jihad_flag', $heartbeat, $this->flagService)
         );
     }
 }