Browse Source

Route subscriber provides dynamic edit link in user menu
heartbeats are now rendered with username beside comments and posts
theming changes
code cleanup

logicp 7 years ago
parent
commit
bbdbbd731f

+ 19 - 0
heartbeat.module

@@ -8,6 +8,7 @@
 
 use Drupal\Core\Routing\RouteMatchInterface;
 use Drupal\Core\Entity\EntityInterface;
+use Drupal\Core\Url;
 use Drupal\Core\Form\FormState;
 use Drupal\node\NodeInterface;
 use Drupal\Core\Session\AccountInterface;
@@ -531,3 +532,21 @@ function heartbeat_page_attachments_alter(array &$attachments) {
 
   $attachments['#attached']['library'][] = 'heartbeat/heartbeat';
 }
+
+
+function heartbeat_link_alter(&$variables) {
+  if (!is_object($variables['text']) && $variables['text'] === 'Account Settings') {
+    $token_service = \Drupal::token();
+    $replacement = $token_service->replace($variables['url']->getUri(), [\Drupal::currentUser()]);
+    unset($variables['url']);
+    $url = Url::fromUserInput('/' . substr($replacement, 5));
+    $variables['url'] = $url;
+  }
+}
+
+/**
+ * Implements hook_form_alter().
+ */
+function heartbeat_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
+
+}

+ 7 - 0
heartbeat.routing.yml

@@ -151,3 +151,10 @@ heartbeat.sub_comment_request:
     _title: 'Sub Comment Block'
   requirements:
     _permission: 'access content'
+
+heartbeat.user_edit:
+  path: '/user/'
+  defaults:
+    _title: 'User Edit'
+  requirements:
+    _permission: 'access content'

+ 4 - 0
heartbeat.services.yml

@@ -25,3 +25,7 @@ services:
     tags:
       - { name: event_subscriber }
 
+  heartbeat.route_subscriber:
+    class: Drupal\heartbeat\Routing\HeartbeatRouteSubscriber
+    tags:
+      - { name: event_subscriber }

+ 12 - 26
js/heartbeat.js

@@ -67,32 +67,17 @@
           Drupal.AjaxCommands.prototype.myfavouritemethodintheworld = function(ajax, response, status) {
             console.dir(response);
             if (response.cid) {
-              let parentCheck = document.getElementById('heartbeat-comment-' + response.cid);
-              let form = parentCheck.querySelector('form');
               console.log('this shit is getting called again');
-
-              if (form == null) {
-                $.ajax({
-                  type: 'POST',
-                  url: '/heartbeat/subcomment/' + response.cid,
-                  success: function (data) {
-                    let parentComment = document.getElementById('heartbeat-comment-' + response.cid);
-                    let insertNode = document.createElement('div');
-                    insertNode.innerHTML = data;
-                    parentComment.appendChild(insertNode);
-
-                    let text = parentComment.querySelector('.form-textarea');
-
-                    // text.addEventListener('keydown', function (e) {
-                    //   console.dir(e);
-                    //   // if (e.keyCode === 13) {
-                    //   //   let submitBtn = parentComment.querySelector('.form-submit');
-                    //   //   submitBtn.click();
-                    //   // }
-                    // });
-                  }
-                })
-              }
+              let parentComment = document.getElementById('heartbeat-comment-' + response.cid);
+              let text = parentComment.querySelector('.form-textarea');
+
+              text.addEventListener('keydown', function (e) {
+                console.dir(e);
+                if (e.keyCode === 13) {
+                  let submitBtn = parentComment.querySelector('.form-submit');
+                  submitBtn.click();
+                }
+              });
             }
           }
         }
@@ -123,6 +108,7 @@
   }
 
   function listenCommentPost() {
+    //TODO is drupal data selector enough? I doubt it.
     let comments = document.querySelectorAll('[data-drupal-selector]');
 
     for (let i = 0; i < comments.length; i++) {
@@ -137,7 +123,7 @@
   function getParent(node) {
     console.dir(node);
     if (node.classList.contains('heartbeat-comment')) {
-      let id = node.id.substr(node.id.indexOf('-')+1);
+      let id = node.id.substr(node.id.indexOf('-') + 1);
       $.ajax({
         type: 'POST',
         url:'/heartbeat/commentupdate/' + id,

+ 4 - 10
src/Controller/HeartbeatController.php

@@ -204,19 +204,13 @@ class HeartbeatController extends ControllerBase implements ContainerInjectionIn
     $subCommentConfig = \Drupal::configFactory()->getEditable('heartbeat_comment.settings');
     $subCommentConfig->set('cid', $cid)->save();
 
-    $muhComment = \Drupal::entityTypeManager()->getStorage('comment')->load($cid);
-    $muhForm = \Drupal::formBuilder()->getForm('\Drupal\heartbeat\Form\HeartbeatSubCommentForm', $muhComment);
-//heartbeat-comment-' + response.cid
     $response = new AjaxResponse();
-//    $response->addCommand(new SubCommentCommand($cid));
-    $response->addCommand(new AppendCommand('#heartbeat-comment-' . $cid, BlockViewBuilder::lazyBuilder('heartbeatsubcommentblock', 'teaser')));
-    return $response;
+    $response->addCommand(new AppendCommand('#heartbeat-comment-' . $cid,
+      BlockViewBuilder::lazyBuilder('heartbeatsubcommentblock', 'teaser')));
+    $response->addCommand(new SubCommentCommand($cid));
 
+    return $response;
 
-//    return [
-//      '#type' => 'markup',
-//      '#markup' => 'Success',
-//    ];
   }
 
   public function subComment() {

+ 1 - 1
src/HeartbeatStreamServices.php

@@ -130,7 +130,7 @@ class HeartbeatStreamServices {
   }
 
   public function createStreamForUids($uids) {
-    return $this->entityTypeManager->getStorage('heartbeat')->loadMultiple($this->entityQuery->get('heartbeat')->condition('status', 1)->condition('uid', $uids, 'IN')->sort('created', 'DESC')->range(0,50)->range(0,50)->execute());
+    return $this->entityTypeManager->getStorage('heartbeat')->loadMultiple($this->entityQuery->get('heartbeat')->condition('status', 1)->condition('uid', $uids, 'IN')->sort('created', 'DESC')->range(0,50)->execute());
   }
 
   public function createStreamByType($type) {

+ 2 - 1
src/Plugin/Block/HeartbeatBlock.php

@@ -140,7 +140,7 @@ class HeartbeatBlock extends BlockBase implements ContainerFactoryPluginInterfac
         $uids[] = $uid->uid;
       }
     }
-      if ($feed !== null) {
+      if ($feed !== null && $this->heartbeatStreamServices) {
       $uids = count($uids) > 1 ? array_unique($uids) : $uids;
         if (!empty($uids)) {
           foreach ($this->heartbeatStreamServices->createStreamForUidsByType($uids, $feed) as $heartbeat) {
@@ -348,6 +348,7 @@ class HeartbeatBlock extends BlockBase implements ContainerFactoryPluginInterfac
         'userId' => $user->id(),
         'timeAgo' => $timeago,
         'id' => $heartbeat->id(),
+        'userName' => $user->getAccountName(),
         'user' => $userView,
         'commentForm' => $form,
         'comments' => $comments,

+ 7 - 0
src/Plugin/Block/HeartbeatHashBlock.php

@@ -6,6 +6,8 @@ use Drupal\comment\Entity\Comment;
 use Drupal\Core\Block\BlockBase;
 use Drupal\Core\Entity\EntityTypeManager;
 use Drupal\Core\Form\FormBuilder;
+use Drupal\Core\Link;
+use Drupal\Core\Url;
 use Drupal\flag\FlagService;
 use Drupal\User\Entity\User;
 use Drupal\Flag\Entity\Flag;
@@ -55,6 +57,9 @@ class HeartbeatHashBlock extends BlockBase implements ContainerFactoryPluginInte
   protected $flagService;
 
   protected $formBuilder;
+
+  protected $timestamp;
+
   /**
    * Construct.
    *
@@ -81,6 +86,7 @@ class HeartbeatHashBlock extends BlockBase implements ContainerFactoryPluginInte
     $this->dateFormatter = $date_formatter;
     $this->flagService = $flag_service;
     $this->formBuilder = $form_builder;
+    $this->timestamp = time();
   }
   /**
    * {@inheritdoc}
@@ -338,6 +344,7 @@ class HeartbeatHashBlock extends BlockBase implements ContainerFactoryPluginInte
       'userId' => $user->id(),
       'timeAgo' => $timeago,
       'id' => $heartbeat->id(),
+      'userName' => $user->getAccountName(),
       'user' => $userView,
       'commentForm' => $form,
       'comments' => $comments,

+ 8 - 1
src/Plugin/Block/HeartbeatMoreBlock.php

@@ -6,6 +6,8 @@ use Drupal\comment\Entity\Comment;
 use Drupal\Core\Block\BlockBase;
 use Drupal\Core\Entity\EntityTypeManager;
 use Drupal\Core\Form\FormBuilder;
+use Drupal\Core\Link;
+use Drupal\Core\Url;
 use Drupal\flag\FlagService;
 use Drupal\User\Entity\User;
 use Drupal\Flag\Entity\Flag;
@@ -55,6 +57,9 @@ class HeartbeatMoreBlock extends BlockBase implements ContainerFactoryPluginInte
   protected $flagService;
 
   protected $formBuilder;
+
+  protected $timestamp;
+
   /**
    * Construct.
    *
@@ -81,6 +86,7 @@ class HeartbeatMoreBlock extends BlockBase implements ContainerFactoryPluginInte
     $this->dateFormatter = $date_formatter;
     $this->flagService = $flag_service;
     $this->formBuilder = $form_builder;
+    $this->timestamp = time();
   }
   /**
    * {@inheritdoc}
@@ -327,12 +333,13 @@ class HeartbeatMoreBlock extends BlockBase implements ContainerFactoryPluginInte
       ]],
       '#create_placeholder' => TRUE,
     ];
-
+//TODO move this method as a static method on Heartbeat entity
     $messages[] = array('heartbeat' => $heartbeat->getMessage()->getValue()[0]['value'],
       'userPicture' => $rendered,
       'userId' => $user->id(),
       'timeAgo' => $timeago,
       'id' => $heartbeat->id(),
+      'userName' => $user->getAccountName(),
       'user' => $userView,
       'commentForm' => $form,
       'comments' => $comments,

+ 31 - 0
src/Routing/HeartbeatRouteSubscriber.php

@@ -0,0 +1,31 @@
+<?php
+/**
+ * Created by IntelliJ IDEA.
+ * User: logicp
+ * Date: 7/1/17
+ * Time: 2:00 AM
+ */
+
+namespace Drupal\heartbeat\Routing;
+
+
+use Drupal\Core\Routing\RouteSubscriberBase;
+use Symfony\Component\Routing\RouteCollection;
+
+class HeartbeatRouteSubscriber extends RouteSubscriberBase {
+
+
+  /**
+   * Alters existing routes for a specific collection.
+   *
+   * @param \Symfony\Component\Routing\RouteCollection $collection
+   *   The route collection for adding routes.
+   */
+  protected function alterRoutes(RouteCollection $collection) {
+    $route = $collection->get('heartbeat.user_edit');
+
+    if ($route !== null) {
+      $route->setPath('/user/' . \Drupal::currentUser()->id() . '/edit');
+    }
+  }
+}

+ 9 - 3
templates/heartbeat-stream.html.twig

@@ -25,7 +25,9 @@
       {#{{ message }}#}
       <div class="heartbeat-message-wrap">
         <div class="heartbeat-owner">
-          <a href="/user/{{ message.userId }}"><img src="{{ message.userPicture }}" /></a>
+          <a href="/user/{{ message.userId }}"><img src="{{ message.userPicture }}" />
+            {{ message.userName }}
+          </a>
           {{ message.user }}
           <div class="time-ago">{{ message.timeAgo }}</div>
         </div>
@@ -44,13 +46,17 @@
           <div class="heartbeat-comments">
             {% for comment in message.comments %}
               <div id="heartbeat-comment-{{ comment.id }}">
-                <span class="comment-owner"><span class="comment-username">{{ comment.username }}</span>{{ comment.owner }}<span class="comment-ago">{{ comment.timeAgo }}</span>
+                <span class="comment-owner">{{ comment.owner }}
+                  <span class="comment-username">{{ comment.username }}</span><br>
+                  <span class="comment-ago">{{ comment.timeAgo }}</span>
                 <span class="comment-like">{{ comment.commentLike}}</span></span>
                 <span class="comment-body">{{ comment.body }}</span>
                 <span class ="sub-comment">
                   {% for subComment in comment.subComments %}
                   <div class="heartbeat-subcomment" id="sub-comment-{{ subComment.id }}">
-                    <span class="sub-comment-owner"><span class="sub-comment-username">{{ subComment.username }}</span>{{ subComment.owner }}<span class="sub-comment-ago">{{ subComment.timeAgo }}</span>
+                    <span class="sub-comment-owner">{{ subComment.owner }}
+                      <span class="sub-comment-username">{{ subComment.username }}</span><br>
+                      <span class="sub-comment-ago">{{ subComment.timeAgo }}</span>
                 <span class="sub-comment-like">{{ subComment.commentLike}}</span></span>
                 <span class="sub-comment-body">{{ subComment.body }}</span>
                   </div>