Browse Source

Improved hook
added theming for Stream Messages

logicp 7 years ago
parent
commit
85a2816199

+ 19 - 0
config/install/heartbeat.heartbeat_type.user_create_node.yml

@@ -0,0 +1,19 @@
+uuid: 67d3ce5b-bf89-4dad-95d9-70a6cde5c504
+langcode: en
+status: true
+dependencies: {  }
+id: user_create_node
+label: 'User Create Node'
+hid: null
+message_id: null
+description: 'Records the event of a user creating a node'
+message: '!user has created a node: !nodetitle'
+message_concat: ''
+perms: 0
+group_type: HEARTBEAT_GROUP_NONE
+arguments: '{"!user":"[user:account-name]","!nodetitle":"[node:title]"}'
+variables:
+  - '[user:account-name]'
+  - '[node:title]'
+attachments: null
+

+ 8 - 0
css/heartbeat.css

@@ -0,0 +1,8 @@
+.token-tree {
+  display: block;
+}
+
+
+.heartbeat-message {
+  border: 1px solid black;
+}

+ 0 - 3
css/heartbeat8.css

@@ -1,3 +0,0 @@
-.token-tree {
-  display: block;
-}

+ 5 - 0
heartbeat.libraries.yml

@@ -3,3 +3,8 @@ treeTable:
   css:
     theme:
       css/heartbeat.css: {}
+heartbeat:
+  version: 1.x
+  css:
+    theme:
+      css/heartbeat.css: {}

+ 40 - 40
heartbeat.module

@@ -74,68 +74,68 @@ function heartbeat_theme_suggestions_heartbeat(array $variables) {
   return $suggestions;
 }
 
+
+
+
 /**
  * Implements hook_entity_insert().
  */
 function heartbeat_entity_insert(EntityInterface $entity) {
 
-  if ($entity instanceof \Drupal\node\Entity\Node) {
+  switch (true) {
+    case $entity instanceof \Drupal\node\Entity\Node:
 
-    $nid = $entity->id();
-    $user = $entity->getOwner();
-    $type = $entity->getEntityTypeId();
-    $media = HeartBeat::mediaFieldTypes($entity->getFields());
+      $user = $entity->getOwner();
+      $media = HeartBeat::mediaFieldTypes($entity->getFields());
 
-    /** @noinspection NestedTernaryOperatorInspection */
-    /** @noinspection ArrayCastingEquivalentInspection */
-  //  $variables = array(
-  //    '!nid' => $nid,
-  //    '!entityType' => $type,
-  //    '!uid' => $user->id(),
-  //    '!user' => $user->getAccountName(),
-  //    'media' => $media,
-  //  );
+      if ($entity->id() && $user->isAuthenticated()) {
 
+        $heartbeatTypeService = \Drupal::service('heartbeat.heartbeattype');
+        $tokenService = \Drupal::service('token');
 
-    if ($entity->id() && $user->isAuthenticated()) {
+        foreach ($heartbeatTypeService->getTypes() as $type) {
 
-      $heartbeatTypeService = \Drupal::service('heartbeat.heartbeattype');
-      $tokenService = \Drupal::service('token');
+          $heartbeatTypeEntity = $heartbeatTypeService->load($type);
+          $arguments = json_decode($heartbeatTypeEntity->getArguments());
 
-      foreach ($heartbeatTypeService->getTypes() as $type) {
+          foreach ($arguments as $key => $argument) {
+            $variables[$key] = $argument;
+          }
 
-        $heartbeatTypeEntity = $heartbeatTypeService->load($type);
-        $arguments = json_decode($heartbeatTypeEntity->getArguments());
+          $preparsedMessageString = strtr($heartbeatTypeEntity->getMessage(), $variables);
 
-        foreach ($arguments as $key => $argument) {
-          $variables[$key] = $argument;
-        }
+          $entities = array(
+            'node' => $entity,
+            'user' => $user,
+          );
 
-        $preparsedMessageString = strtr($heartbeatTypeEntity->getMessage(), $variables);
+          $heartbeatMessage = Heartbeat::buildMessage($tokenService, $preparsedMessageString, $entities, $media);
 
-        $entities = array(
-          'node' => $entity,
-          'user' => $user,
-        );
+          //      $translatedMessage = t($messageTemplate);
 
-        $heartbeatMessage = Heartbeat::buildMessage($tokenService, $preparsedMessageString, $entities, $media);
+          $heartbeatActivity = Heartbeat::create([
+            'type' => $heartbeatTypeEntity->id(),
+            'uid' => $user->id(),
+            'nid' => $entity->id(),
+            'name' => 'Dev Test',
+          ]);
 
-  //      $translatedMessage = t($messageTemplate);
+          $heartbeatActivity->setMessage($heartbeatMessage);
+          $heartbeatActivity->save();
 
-        $heartbeatActivity = Heartbeat::create([
-          'type' => $heartbeatTypeEntity->id(),
-          'uid' => $user->id(),
-          'nid' => $entity->id(),
-          'name' => 'Dev Test',
-        ]);
+        }
+      }
 
-        $heartbeatActivity->setMessage($heartbeatMessage);
-        $heartbeatActivity->save();
+      break;
+    case $entity instanceof \Drupal\user\Entity\User:
+//      $userService = \Drupal\User\Entity\user
 
-      }
-    }
+      break;
+    case $entity instanceof \Drupal\flag\Entity\Flag:
+      break;
   }
 
+
 }
 
 function heartbeat_node_presave(NodeInterface $node) {

+ 1 - 0
src/Controller/StreamTestController.php

@@ -90,6 +90,7 @@ class StreamTestController extends ControllerBase {
     return [
       '#theme' => 'heartbeat_stream',
       '#messages' => array_reverse($messages),
+      '#attached' => array('library' => 'heartbeat/heartbeat')
     ];
   }
 

+ 6 - 0
src/Entity/HeartbeatStream.php

@@ -361,6 +361,12 @@ class HeartbeatStream extends RevisionableContentEntityBase implements Heartbeat
       ->setDisplayConfigurable('form', TRUE)
       ->setDisplayConfigurable('view', TRUE);
 
+    $fields['path'] = BaseFieldDefinition::create('string')
+      ->setLabel(t('Path'))
+      ->setSettings(array(
+        'max_length' => 255,
+      ));
+
     $fields['name'] = BaseFieldDefinition::create('string')
       ->setLabel(t('Name'))
       ->setDescription(t('The name of the Heartbeat stream entity.'))

+ 21 - 2
src/Form/HeartbeatStreamForm.php

@@ -27,6 +27,15 @@ class HeartbeatStreamForm extends ContentEntityForm {
   /**
    * {@inheritdoc}
    */
+
+
+
+
+
+
+
+
+
   public static function create(ContainerInterface $container) {
     return new static(
       $container->get('heartbeat.heartbeattype'),
@@ -64,6 +73,10 @@ class HeartbeatStreamForm extends ContentEntityForm {
    */
   public function buildForm(array $form, FormStateInterface $form_state) {
     /* @var $entity \Drupal\heartbeat\Entity\HeartbeatStream */
+
+    if ($entity == NULL) {
+      $entity = &$this->entity;
+    }
     $form = parent::buildForm($form, $form_state);
 
     if (!$this->entity->isNew()) {
@@ -79,11 +92,14 @@ class HeartbeatStreamForm extends ContentEntityForm {
 
       '#type' => 'checkboxes',
       '#options' => $this->heartbeatTypeService->getTypes(),
-
       '#title' => $this->t('Please select all the Heartbeat Types you wish to include in this stream'),
-
     );
 
+    $form['path'] = array(
+      '#type' => 'textfield',
+      '#description' => 'The relative url path for this Heartbeat Stream',
+      '#default' => $entity->getPath(),
+    );
 
     return $form;
   }
@@ -111,6 +127,9 @@ class HeartbeatStreamForm extends ContentEntityForm {
       foreach ($form_state->getValue('types') as $type) {
         $entity->get('types')->appendItem($type);
       }
+
+      $entity->setPath($form_state->getValue('path'));
+
       $entity->save();
     }
 

+ 4 - 2
templates/heartbeat-stream.html.twig

@@ -15,13 +15,15 @@
  * @ingroup themeable
  */
 #}
-<div{{ attributes.addClass('heartbeat-stream') }}>
+<div class="heartbeat-stream">
   {% if content %}
     {{- content -}}
   {% endif %}
   {% autoescape false %}
   {% for message in messages %}
-    {{ message }} <br>
+    <div{{ attributes.addClass('heartbeat-message') }}>
+      {{ message }} <br>
+    </div>
   {% endfor %}
   {% endautoescape %}
 </div>

+ 1 - 0
templates/heartbeat_stream.html.twig

@@ -18,5 +18,6 @@
 <div{{ attributes.addClass('heartbeat_stream') }}>
   {% if content %}
     {{- content -}}
+    <h1> The Jiggaman Cometh </h1>
   {% endif %}
 </div>