Explorar o código

Major development
Many helper methods added to Heartbeat class
hook_entity_insert iterates over HeartbeatTypes, builds message templates and then parses the template to map variable values to keywords in the message

logicp %!s(int64=8) %!d(string=hai) anos
pai
achega
345ae5b177

+ 35 - 1
heartbeat8.module

@@ -11,6 +11,7 @@ use Drupal\Core\Entity\EntityInterface;
 use Drupal\node\NodeInterface;
 use Drupal\Core\Session\AccountInterface;
 use Drupal\Core\Entity;
+use Drupal\heartbeat8\Entity\FILE_FIELD;
 use Drupal\heartbeat8\Entity\Heartbeat;
 use Drupal\heartbeat8\Entity\HeartbeatType;
 
@@ -72,14 +73,42 @@ function heartbeat8_theme_suggestions_heartbeat(array $variables) {
  */
 function heartbeat8_entity_insert(EntityInterface $entity) {
 
+if ($entity instanceof \Drupal\node\Entity\Node) {
+
   $nid = $entity->id();
+  $user = $entity->getOwner();
+  $type = $entity->getEntityTypeId();
+  $media = HeartBeat::mediaFieldTypes($entity->getFields());
+
+  /** @noinspection NestedTernaryOperatorInspection */
+  /** @noinspection ArrayCastingEquivalentInspection */
+  $variables = array(
+    '!nid' => $nid,
+    '!entityType' => $type,
+    '!uid' => $user->id(),
+    '!user' => $user->getAccountName(),
+    'media' => $media,
+  );
+
+}
 
   if ($nid) {
 
     $heartbeatTypeService = \Drupal::service('heartbeat8.heartbeattype');
 
     foreach ($heartbeatTypeService->getTypes() as $type) {
-      $heartbeatTypeEntity = \Drupal::entityTypeManager()->getStorage('heartbeat_type')->load($type);
+
+      $heartbeatTypeEntity = $heartbeatTypeService->load($type);
+
+      $messageTemplate = Heartbeat::buildMessage($heartbeatTypeEntity, $media);
+
+//      $translatedMessage = t($messageTemplate);
+
+      $heartbeatMarkup = Heartbeat::parseMessage($messageTemplate, $variables);
+
+//      $heartbeatActivity = new Heartbeat();
+//      $heartbeatActivity->setName();
+//      $heartbeatActivity->save();
 
 
     }
@@ -87,6 +116,11 @@ function heartbeat8_entity_insert(EntityInterface $entity) {
 
 }
 
+function heartbeat8_node_presave(NodeInterface $node) {
+  $uid = $node->getOwnerId();
+  $stuffTOdo = null;
+}
+
 //TODO Add heartbeat language to Javascript
 //TODO Determine necessity of polling
 //Add

+ 1 - 1
heartbeat8.services.yml

@@ -1,7 +1,7 @@
 services:
   heartbeat8.heartbeattype:
     class: Drupal\heartbeat8\HeartbeatTypeServices
-    arguments: ['@entity.query']
+    arguments: ['@entity_type.manager', '@entity.query']
 
   heartbeatstream:
     class: Drupal\heartbeat8\HeartbeatStreamServices

+ 118 - 1
src/Entity/Heartbeat.php

@@ -92,6 +92,8 @@ const HEARTBEAT_GROUP_NONE = 11;
 const HEARTBEAT_GROUP_SINGLE = 12;
 const HEARTBEAT_GROUP_SUMMARY = 13;
 
+const FILE_FIELD = 'Drupal\file\Plugin\Field\FieldType\FileFieldItemList';
+
 
 
 class Heartbeat extends RevisionableContentEntityBase implements HeartbeatInterface {
@@ -108,6 +110,7 @@ class Heartbeat extends RevisionableContentEntityBase implements HeartbeatInterf
     );
   }
 
+
   /**
    * {@inheritdoc}
    */
@@ -360,8 +363,122 @@ class Heartbeat extends RevisionableContentEntityBase implements HeartbeatInterf
    * @return
    *   The number of activities whose heartbeat type field was modified.
    */
-  function heartbeat_type_update_nodes($old_id, $new_id) {
+  public function heartbeat_type_update_nodes($old_id, $new_id) {
     return \Drupal::entityManager()->getStorage('heartbeat')->updateType($old_id, $new_id);
   }
 
+
+  /**
+   * Builds a message template for a given HeartbeatType
+   *
+   * @param HeartbeatType $heartbeatType
+   * @param null $mediaData
+   * @return null|string
+   */
+  public static function buildMessage(HeartbeatType $heartbeatType, $mediaData = NULL) {
+//!username has added !node_type !node_title. <a href="/node/"><img src="/sites/default/files/!node_image"/></a>
+
+    /** @noinspection NestedTernaryOperatorInspection */
+    $message = $heartbeatType->get('message') . '<a href="/node/!nid">';
+    $message .= $mediaData ? self::buildMediaMarkup($mediaData) : '';
+    $message .= '</a>';
+
+    return $message;
+  }
+
+
+  private static function buildMediaMarkup($mediaData) {
+
+    $markup = '';
+
+    foreach ($mediaData as $media) {
+      $markup .= self::mediaTag($media->type, $media->path);
+    }
+
+    return $markup;
+  }
+
+  private static function mediaTag($type, $filePath) {
+    return '<'. $type . ' src="' . $filePath . '" / >';
+  }
+
+
+  /**
+   * Returns class of argument
+   *
+   * @param $field
+   * @return string
+   */
+  public static function findClass($field) {
+    return get_class($field);
+  }
+
+
+  /**
+   * Returns an array of classes for array argument
+   * @param $fields
+   * @return array
+   */
+  public static function findAllMedia($fields) {
+    return array_map(array(get_called_class(), 'findClass'), $fields);
+  }
+
+
+  /**
+   * Returns all media types for an array of fields
+   *
+   * @param $fields
+   * @return array
+   */
+  public static function mediaFieldTypes($fields) {
+
+    $types = array();
+
+    foreach ($fields as $field) {
+      if ($field instanceof \Drupal\file\Plugin\Field\FieldType\FileFieldItemList) {
+
+        if ($field->getFieldDefinition()->getType() === 'image' ||
+            $field->getFieldDefinition()->getType() === 'video' ||
+            $field->getFieldDefinition()->getType() === 'audio') {
+
+          $fieldValue = $field->getValue();
+          $fileId = $fieldValue[0]['target_id'];
+          $file = \Drupal::entityTypeManager()->getStorage('file')->load($fileId);
+
+          if ($file !== NULL && is_object($file)) {
+
+            $mediaObject = self::createHeartbeatMedia($field->getFieldDefinition()->getType(), $file->url());
+            $types[] = $mediaObject;
+
+          } else {
+            continue;
+          }
+        }
+      }
+    }
+    return $types;
+  }
+
+
+  /**
+   * Parses a HeartbeatType message template and maps
+   * variable values onto matching keywords
+   *
+   * @param $translatedMessage
+   * @param $variables
+   * @return string
+   */
+  public static function parseMessage($translatedMessage, $variables) {
+    return strtr($translatedMessage, $variables);
+  }
+
+  public static function createHeartbeatMedia($type, $path) {
+
+    $mediaObject = new \stdClass();
+    $mediaObject->type = $type;
+    $mediaObject->path = $path;
+
+    return $mediaObject;
+  }
+
 }

+ 2 - 2
src/Entity/HeartbeatType.php

@@ -110,7 +110,7 @@ class HeartbeatType extends ConfigEntityBundleBase implements HeartbeatTypeInter
    *  The template message serving as the foundation of each message structure of this stream type
    */
   public function setMessage($message) {
-    $this->message = $message;
+    $this->set('message', $message);
   }
 
   /**
@@ -120,7 +120,7 @@ class HeartbeatType extends ConfigEntityBundleBase implements HeartbeatTypeInter
    *  The Stream's message
    */
   public function getMessage() {
-    return $this->message;
+    return $this->get('message');
   }
 
   /**

+ 17 - 1
src/HeartbeatTypeServices.php

@@ -1,6 +1,7 @@
 <?php
 
 namespace Drupal\heartbeat8;
+use Drupal\Core\Entity\EntityTypeManager;
 use Drupal\Core\Entity\Query\QueryFactory;
 
 /**
@@ -10,16 +11,27 @@ use Drupal\Core\Entity\Query\QueryFactory;
  */
 class HeartbeatTypeServices {
 
+  /**
+   * Drupal\Core\Entity\EntityTypeManager definition.
+   *
+   * @var EntityTypeManager
+   */
+  protected $entityTypeManager;
+
   /**
    * Drupal\Core\Entity\Query\QueryFactory definition.
    *
    * @var \Drupal\Core\Entity\Query\QueryFactory
    */
   protected $entityQuery;
+
   /**
    * Constructor.
+   * @param EntityTypeManager $entityTypeManager
+   * @param QueryFactory $entity_query
    */
-  public function __construct(QueryFactory $entity_query) {
+  public function __construct(EntityTypeManager $entityTypeManager, QueryFactory $entity_query) {
+    $this->entityTypeManager = $entityTypeManager;
     $this->entityQuery = $entity_query;
   }
 
@@ -27,4 +39,8 @@ class HeartbeatTypeServices {
   public function getTypes() {
     return $this->entityQuery->get('heartbeat_type')->execute();
   }
+
+  public function load($id) {
+    return $this->entityTypeManager->getStorage('heartbeat_type')->load($id);
+  }
 }