Browse Source

Major update
Heartbeat parsing improved
better messages
comments

logicp 7 years ago
parent
commit
10dd1cd1d9

+ 9 - 0
config/install/comment.type.heartbeat_comment.yml

@@ -0,0 +1,9 @@
+uuid: 49e8f841-2c5d-49c7-96ff-974c810afdeb
+langcode: en
+status: true
+dependencies: {  }
+id: heartbeat_comment
+label: 'Heartbeat Comment'
+target_entity_type_id: heartbeat
+description: 'Comments applied to a Heartbeat'
+

+ 0 - 2
css/heartbeat.css

@@ -9,8 +9,6 @@
 }
 
 .heartbeat-message img {
-  max-width: 286px;
-  margin: 8px;
   display: block;
 }
 

+ 22 - 1
heartbeat.routing.yml

@@ -15,6 +15,27 @@ heartbeat.test_controller_start:
     _title: 'run'
   requirements:
     _permission: 'access content'
+heartbeat.heartbeat_save:
+  path: '/heartbeat/heartbeats/save'
+  defaults:
+    _controller: '\Drupal\heartbeat\Controller\TestController::saveHeartbeats'
+    _title: 'run'
+  requirements:
+    _permission: 'access content'
+heartbeat.heartbeat_load:
+  path: '/heartbeat/heartbeats/load'
+  defaults:
+    _controller: '\Drupal\heartbeat\Controller\TestController::getHeartbeats'
+    _title: 'run'
+  requirements:
+    _permission: 'access content'
+heartbeat.heartbeat_delete:
+  path: '/heartbeat/heartbeats/delete'
+  defaults:
+    _controller: '\Drupal\heartbeat\Controller\TestController::deleteHeartbeats'
+    _title: 'run'
+  requirements:
+    _permission: 'access content'
 
 #heartbeat.stream_test_controller_stream:
 #  path: '/nodeactivity'
@@ -90,4 +111,4 @@ heartbeat.heartbeat_update_feed_form:
     _title: 'HeartbeatUpdateFeedForm'
   requirements:
     _access: 'TRUE'
-  
+

+ 69 - 3
src/Controller/TestController.php

@@ -5,10 +5,12 @@ namespace Drupal\heartbeat\Controller;
 use Drupal\Core\Controller\ControllerBase;
 use Drupal\Core\Database\Database;
 use Drupal\Core\Entity\EntityTypeManager;
+use Drupal\Core\Entity\Query\QueryFactory;
 use Drupal\flag\FlagService;
 use Symfony\Component\DependencyInjection\ContainerInterface;
 use Drupal\heartbeat\HeartbeatTypeServices;
 use Drupal\heartbeat\HeartbeatStreamServices;
+use Drupal\heartbeat\Entity\Heartbeat;
 use Drupal\statusmessage\StatusTwitter;
 use Symfony\Component\Routing\Route;
 use Symfony\Component\Routing\RouteCollection;
@@ -34,14 +36,17 @@ class TestController extends ControllerBase {
    * @var HeartbeatStreamServices
    */
   protected $heartbeatStream;
+
+  protected $entityQuery;
   /**
    * {@inheritdoc}
    */
-  public function __construct(HeartbeatTypeServices $heartbeat_heartbeattype, HeartbeatStreamServices $heartbeatstream, FlagService $flag_service, EntityTypeManager $entity_type_manager) {
+public function __construct(HeartbeatTypeServices $heartbeat_heartbeattype, HeartbeatStreamServices $heartbeatstream, FlagService $flag_service, EntityTypeManager $entity_type_manager, QueryFactory $entity_query) {
     $this->heartbeat_heartbeattype = $heartbeat_heartbeattype;
     $this->heartbeatStream = $heartbeatstream;
     $this->flagService = $flag_service;
     $this->entityTypeManager = $entity_type_manager;
+    $this->entityQuery = $entity_query;
   }
 
   /**
@@ -52,8 +57,8 @@ class TestController extends ControllerBase {
       $container->get('heartbeat.heartbeattype'),
       $container->get('heartbeatstream'),
       $container->get('flag'),
-      $container->get('entity_type.manager')
-
+      $container->get('entity_type.manager'),
+      $container->get('entity.query')
     );
   }
 
@@ -78,4 +83,65 @@ class TestController extends ControllerBase {
     ];
   }
 
+  public function saveHeartbeats() {
+    $heartbeats = $this->entityQuery->get("heartbeat")->execute();
+    $data = array();
+    foreach ($heartbeats as $hid) {
+      $beat = $this->entityTypeManager->getStorage("heartbeat")->load($hid);
+      $data[] = $beat;
+    }
+
+    $result = '';
+
+     $result = file_put_contents("public://heartbeats.dat", serialize($data)) ? 'Saved Heartbeats' : 'Error saving heartbeats';
+
+    return [
+      '#type' => 'markup',
+      '#markup' => $this->t($result),
+    ];
+
+
+  }
+
+  public function getHeartbeats() {
+
+    $data = file_get_contents("public://heartbeats.dat");
+    $heartbeats = unserialize($data);
+    $errors = false;
+    if (is_array($heartbeats)) {
+      $heartbeats = array_reverse($heartbeats);
+      foreach ($heartbeats as $heartbeat) {
+
+        $message = $heartbeat->getMessage();
+        $heartbeatActivity = Heartbeat::create([
+          'type' => $heartbeat->id(),
+          'uid' => $heartbeat->getOwnerId(),
+          'nid' => $heartbeat->getNid()->getValue()[0]['target_id'],
+          'name' => 'Dev Test',
+          'type' => $heartbeat->getType(),
+          'message' => $heartbeat->getMessage()->getValue()[0]['value']
+        ]);
+
+        if (!$heartbeatActivity->save()) {
+          $errors = true;
+        }
+      }
+    }
+    $result = $errors ? 'Error restoring Heartbeats' : 'Heartbeats restored';
+
+    return [
+      '#type' => 'markup',
+      '#markup' => $this->t($result),
+    ];
+
+  }
+
+  public function deleteHeartbeats() {
+    $entities = \Drupal::service("entity.query")->get("heartbeat")->execute();
+    foreach($entities as $entity) {
+      $heartbeat = \Drupal::service("entity_type.manager")->getStorage("heartbeat")->load($entity);
+      $heartbeat->delete();
+    }
+  }
+
 }

+ 58 - 2
src/Entity/Heartbeat.php

@@ -12,6 +12,7 @@ use Drupal\Core\Url;
 use Drupal\Core\Link;
 use Drupal\Core\Database\Database;
 use Drupal\taxonomy\Entity\Term;
+use Drupal\user\Entity\User;
 use Drupal\user\UserInterface;
 
 /**
@@ -346,6 +347,48 @@ class Heartbeat extends RevisionableContentEntityBase implements HeartbeatInterf
       ))
       ->setDisplayConfigurable('view', TRUE);
 
+    $fields['comments'] = BaseFieldDefinition::create('comment')
+      ->setLabel(t('Kommentare'))
+      ->setDescription(t('Kommentare.'))
+      ->setCardinality(BaseFieldDefinition::CARDINALITY_UNLIMITED)
+      ->setSettings(
+        array(
+          'default_mode'=> 1,
+          'per_page'=>50,
+          'anonymous'=> 0,
+          'form_location'=>1,
+          'preview'=> 1,
+          'comment_type'=>'heartbeat_comment',
+          'locked'=>false,
+
+        ))
+      ->setDefaultValue(
+        array(
+          'status'=>2,
+          'cid'=>0,
+          'last_comment_timestamp'=> 0,
+          'last_comment_name'=> null,
+          'last_comment_uid'=> 0,
+          'comment_count'=> 0,
+        )
+      )
+      ->setDisplayOptions('form', array(
+        'type' => 'comment_default',
+        'settings' => array(
+          'form_location' => 1,
+          'default_mode'=> 1,
+          'per_page'=>50,
+          'anonymous'=> 0,
+          'preview'=> 1,
+          'comment_type'=>'heartbeat_comment',
+          'locked'=>false,
+
+        ),
+        'weight' => 1,
+      ))
+      ->setDisplayConfigurable('form', TRUE)
+      ->setDisplayConfigurable('view', TRUE);
+
 
     $fields['status'] = BaseFieldDefinition::create('boolean')
       ->setLabel(t('Publishing status'))
@@ -430,12 +473,15 @@ class Heartbeat extends RevisionableContentEntityBase implements HeartbeatInterf
     $arbitrarious = 'nothing at all';
     $naul = 'nullll';
 
+    $preparsedMessage = self::wrapOwner($preparsedMessage, $entities);
 
     switch (true) {
 
       case $entityType === 'node':
 
-        $parsedMessage = $tokenService->replace($preparsedMessage . '<a class="heartbeat-node" href="/node/[node:nid]">', $entities);
+//        $parsedMessage = $tokenService->replace($preparsedMessage . '<a class="heartbeat-node" href="/node/[node:nid]">', $entities);
+        $parsedMessage = $tokenService->replace($preparsedMessage, $entities);
+
         if (strpos($parsedMessage, '#')) {
           self::parseHashtags($parsedMessage);
         }
@@ -445,7 +491,7 @@ class Heartbeat extends RevisionableContentEntityBase implements HeartbeatInterf
         /** @noinspection NestedTernaryOperatorInspection */
         $message = $parsedMessage;
         $message .= $mediaData ? self::buildMediaMarkup($mediaData) : '';
-        $message .= '</a>';
+//        $message .= '</a>';
 
         return $message;
         break;
@@ -772,6 +818,16 @@ class Heartbeat extends RevisionableContentEntityBase implements HeartbeatInterf
     return $names;
   }
 
+  private static function wrapOwner($message, $entities) {
+    foreach ($entities as $entity) {
+      if ($entity instanceof User) {
+        return str_replace(
+          '[user:account-name]',
+          '<a class="heartbeat-user" href="user/' . $entity->id() . '">[user:account-name]</a>', $message);
+      }
+    }
+    return $message;
+  }
 
   /**
    * Updates the friendship status of these two users

+ 1 - 1
src/HeartbeatStreamServices.php

@@ -175,7 +175,7 @@ class HeartbeatStreamServices {
     $stream = $this->entityTypeManager->getStorage('heartbeat_stream')->load(array_values($this->loadStream($type))[0]);
     $uids[] = $currentUid;
     return $this->entityTypeManager->getStorage('heartbeat')->loadMultiple($this->entityQuery->get('heartbeat')->condition('status', 1)->condition('revision_created', $this->latestTimestamp, '>')->condition('type', array_column($stream->getTypes(), 'target_id'), 'IN')->condition('uid', $uids, 'IN')->sort('created', 'DESC')->execute());
-
+//range(0,50)->=pppp
   }
 
 }

+ 23 - 12
src/Plugin/Block/HeartbeatBlock.php

@@ -4,6 +4,7 @@ namespace Drupal\heartbeat\Plugin\Block;
 
 use Drupal\Core\Block\BlockBase;
 use Drupal\Core\Entity\EntityTypeManager;
+use Drupal\Core\Form\FormBuilder;
 use Drupal\flag\FlagService;
 use Drupal\User\Entity\User;
 use Drupal\Flag\Entity\Flag;
@@ -52,6 +53,8 @@ class HeartbeatBlock extends BlockBase implements ContainerFactoryPluginInterfac
   protected $dateFormatter;
 
   protected $flagService;
+
+  protected $formBuilder;
   /**
    * Construct.
    *
@@ -68,7 +71,7 @@ class HeartbeatBlock extends BlockBase implements ContainerFactoryPluginInterfac
         $plugin_definition,
         HeartbeatTypeServices $heartbeat_heartbeattype,
 	HeartbeatStreamServices $heartbeatstream,
-	HeartbeatService $heartbeat, EntityTypeManager $entity_type_manager, DateFormatter $date_formatter, FlagService $flag_service
+	HeartbeatService $heartbeat, EntityTypeManager $entity_type_manager, DateFormatter $date_formatter, FlagService $flag_service, FormBuilder $form_builder
   ) {
     parent::__construct($configuration, $plugin_id, $plugin_definition);
     $this->heartbeatTypeServices = $heartbeat_heartbeattype;
@@ -77,6 +80,7 @@ class HeartbeatBlock extends BlockBase implements ContainerFactoryPluginInterfac
     $this->entityTypeManager = $entity_type_manager;
     $this->dateFormatter = $date_formatter;
     $this->flagService = $flag_service;
+    $this->formBuilder = $form_builder;
   }
   /**
    * {@inheritdoc}
@@ -91,7 +95,8 @@ class HeartbeatBlock extends BlockBase implements ContainerFactoryPluginInterfac
       $container->get('heartbeat'),
       $container->get('entity_type.manager'),
       $container->get('date.formatter'),
-      $container->get('flag')
+      $container->get('flag'),
+      $container->get('form_builder')
     );
   }
 
@@ -159,13 +164,17 @@ class HeartbeatBlock extends BlockBase implements ContainerFactoryPluginInterfac
 
       $timeago = $this->dateFormatter->formatInterval(REQUEST_TIME - $heartbeat->getCreatedTime());
       $user = $heartbeat->getOwner();
-      $flag = $this->flagService->getFlagById("friendship");
-      $flagLink = $flag->getLinkTypePlugin()->getAsLink($flag, $user);
-      $flagUrl = $flagLink->getUrl()->toString();
-      $flagText = $flagLink->getText();
+//      $rendered = $this->entityTypeManager->getViewBuilder('user')->view($user, 'full');
+      $userView = user_view($user, 'compact');
+//      $flag = $this->flagService->getFlagById("friendship");
+//      $flagLink = $flag->getLinkTypePlugin()->getAsLink($flag, $user);
+//      $flagUrl = $flagLink->getUrl()->toString();
+//      $flagText = $flagLink->getText();
       $profilePic = $user->get('user_picture')->getValue()[0]['target_id'];
-      $flagRenderable = $flagLink->toRenderable();
-      $renderArray = $flagRenderable['#url']->toRenderArray();
+
+//      $commentForm = $this->formBuilder->getForm('Drupal\comment\CommentForm', $heartbeat);
+
+//      $flagRenderable = $flagLink->toRenderable();
 
       if ($profilePic === null) {
         $profilePic = 86;
@@ -186,10 +195,12 @@ class HeartbeatBlock extends BlockBase implements ContainerFactoryPluginInterfac
         'userPicture' => $rendered,
         'userId' => $user->id(),
         'timeAgo' => $timeago,
-        'friendFlag' => $flagUrl,
-        'friendFlagText' => $flagText,
-        'flagId' => $flag->id(),
-        'userId' => $user->id(),
+        'id' => $heartbeat->id(),
+//        'friendFlag' => $flagUrl,
+//        'friendFlagText' => $flagText,
+//        'flagId' => $flag->id(),
+        'user' => $userView,
+//        'commentForm' => $commentForm
         );
     }
 }

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

@@ -21,9 +21,18 @@
   {% endif %}
   {% autoescape false %}
   {% for message in messages %}
-    <div{{ attributes.addClass('heartbeat-message') }}>
-      <div class="heartbeat-owner"><a href="/user/{{ message.userId }}"><img src="{{ message.userPicture }}" /></a><div class"flag {{ message.flagId }} flag-{{ message.flagId }}-{{ message.userId }}"><a href="{{ message.friendFlag }}" class="use-ajax" rel="nofollow"> {{ message.friendFlagText }} </a></div><div class="time-ago">{{ message.timeAgo }} ago</div></div>
-      {{ message.heartbeat }}<br>
+    <div{{ attributes.addClass('heartbeat-message') }} id="heartbeat-{{ message.id }}">
+      <div class="heartbeat-owner">
+        <a href="/user/{{ message.userId }}"><img src="{{ message.userPicture }}" /></a>
+        {{ message.user }}
+        <div class="time-ago">{{ message.timeAgo }} ago</div>
+      </div>
+      <div class="heartbeat-content">
+      {{ message.heartbeat }}
+      </div>
+      <div class="heartbeat-comment">
+        {{ message.commentForm }}
+      </div>
     </div>
   {% endfor %}
   {% endautoescape %}