Browse Source

hook_entity_insert
adding hook for inserting Heartbeat data

logicp 8 years ago
parent
commit
d02764ec5c

+ 16 - 5
heartbeat8.module

@@ -5,13 +5,16 @@
  * Contains heartbeat8.module.
  */
 
-namespace Drupal\heartbeat8;
 
 use Drupal\Core\Routing\RouteMatchInterface;
 use Drupal\Core\Entity\EntityInterface;
+use Drupal\node\NodeInterface;
+use Drupal\Core\Session\AccountInterface;
+use Drupal\Core\Entity;
 use Drupal\heartbeat8\Entity\Heartbeat;
 use Drupal\heartbeat8\Entity\HeartbeatType;
 
+
 //TODO include Streams (Entities already added with use statements on lines 9-10)
 
 // Always block from display
@@ -92,17 +95,25 @@ function heartbeat8_theme_suggestions_heartbeat(array $variables) {
  */
 function heartbeat8_entity_insert(EntityInterface $entity) {
 
-  $heartbeatTypeService = \Drupal::service('heartbeat8.heartbeattype');
+  $thisiswhere = 'ithappens';
+  $nid = $entity->id();
+
+  if ($nid) {
+    $heartbeatTypeService = \Drupal::service('heartbeat8.heartbeattype');
 
-  foreach ($heartbeatTypeService->getTypes() as $type) {
-    $heartbeatTypeEntity = \Drupal::entityTypeManager()->getStorage('heartbeat_type')->load($type);
+    foreach ($heartbeatTypeService->getTypes() as $type) {
+      $heartbeatTypeEntity = \Drupal::entityTypeManager()->getStorage('heartbeat_type')->load($type);
 
 
+    }
   }
 
-}
+  \Drupal::logger('content')->notice('Entity Insert
+   called !');
 
 
+}
+
 //TODO Add heartbeat language to Javascript
 //TODO Determine necessity of polling
 //Add

+ 5 - 1
heartbeat8.services.yml

@@ -5,5 +5,9 @@ services:
 
   heartbeatstream:
     class: Drupal\heartbeat8\HeartbeatStreamServices
-    arguments: ["@entity_type.manager", "@entity_type.repository"]
+    arguments: ["@entity_type.manager", "@entity_type.repository", '@entity.query']
+
+  heartbeatstreamalt:
+    class: Drupal\heartbeat8\HeartbeatAltServices
+    arguments: ['@entity.query', '@entity.manager', '@entity_type.manager', '@entity_type.repository', '@heartbeat8.heartbeattype']
 

+ 16 - 8
src/Controller/TestController.php

@@ -17,14 +17,14 @@ class TestController extends ControllerBase {
   /**
    * Drupal\heartbeat8\HeartbeatTypeServices definition.
    *
-   * @var Drupal\heartbeat8\HeartbeatTypeServices
+   * @var HeartbeatTypeServices
    */
   protected $heartbeat8_heartbeattype;
 
   /**
    * Drupal\heartbeat8\HeartbeatStreamServices definition.
    *
-   * @var Drupal\heartbeat8\HeartbeatStreamServices
+   * @var HeartbeatStreamServices
    */
   protected $heartbeatstream;
   /**
@@ -53,12 +53,20 @@ class TestController extends ControllerBase {
    */
   public function start($arg) {
 
-    $streamEntity = $this->heartbeatstream->getEntityById(1);
-    $types = $streamEntity->get('types');
-    $i = 1;
-    foreach($types->getValue() as $heartbeatType) {
-      $arg .= '   ' . $i . '. ' . $heartbeatType['target_id'];
-      $i++;
+    $streamEntities = $this->heartbeatstream->loadAllEntities()->execute();
+
+    foreach ($streamEntities as $streamEntityId) {
+
+      $streamEntity = $this->heartbeatstream->getEntityById($streamEntityId);
+      $types = $streamEntity->get('types');
+      $arg .= 'Stream::   ' . $streamEntity->id();
+
+      $i = 1;
+
+      foreach ($types->getValue() as $heartbeatType) {
+        $arg .= '   ' . $i . '. ' . $heartbeatType['target_id'];
+        $i++;
+      }
     }
 
     $heartbeatTypeService = \Drupal::service('heartbeat8.heartbeattype');

+ 58 - 0
src/HeartbeatAltServices.php

@@ -0,0 +1,58 @@
+<?php
+
+namespace Drupal\heartbeat8;
+use Drupal\Core\Entity\Query\QueryFactory;
+use Drupal\Core\Entity\EntityManager;
+use Drupal\Core\Entity\EntityTypeManager;
+use Drupal\Core\Entity\EntityTypeRepository;
+use Drupal\heartbeat8\HeartbeatTypeServices;
+
+/**
+ * Class HeartbeatAltServices.
+ *
+ * @package Drupal\heartbeat8
+ */
+class HeartbeatAltServices {
+
+//  /**
+//   * Drupal\Core\Entity\Query\QueryFactory definition.
+//   *
+//   * @var \Drupal\Core\Entity\Query\QueryFactory
+//   */
+//  protected $;
+//  /**
+//   * Drupal\Core\Entity\EntityManager definition.
+//   *
+//   * @var \Drupal\Core\Entity\EntityManager
+//   */
+//  protected $;
+//  /**
+//   * Drupal\Core\Entity\EntityTypeManager definition.
+//   *
+//   * @var \Drupal\Core\Entity\EntityTypeManager
+//   */
+//  protected $;
+//  /**
+//   * Drupal\Core\Entity\EntityTypeRepository definition.
+//   *
+//   * @var \Drupal\Core\Entity\EntityTypeRepository
+//   */
+//  protected $;
+//  /**
+//   * Drupal\heartbeat8\HeartbeatTypeServices definition.
+//   *
+//   * @var \Drupal\heartbeat8\HeartbeatTypeServices
+//   */
+//  protected $;
+//  /**
+//   * Constructor.
+//   */
+//  public function __construct(QueryFactory $entity_query, EntityManager $entity_manager, EntityTypeManager $entity_type_manager, EntityTypeRepository $entity_type_repository, HeartbeatTypeServices $heartbeat8_heartbeattype) {
+//    $this-> = $entity_query;
+//    $this-> = $entity_manager;
+//    $this-> = $entity_type_manager;
+//    $this-> = $entity_type_repository;
+//    $this-> = $heartbeat8_heartbeattype;
+//  }
+
+}

+ 36 - 9
src/HeartbeatStreamServices.php

@@ -4,6 +4,7 @@ namespace Drupal\heartbeat8;
 
 use Drupal\Core\Entity\EntityTypeManager;
 use Drupal\Core\Entity\EntityTypeRepository;
+use Drupal\Core\Entity\Query\QueryFactory;
 
 /**
  * Class HeartbeatStreamServices.
@@ -15,31 +16,45 @@ class HeartbeatStreamServices {
   /**
    * Drupal\Core\Entity\EntityTypeManager definition.
    *
-   * @var Drupal\Core\Entity\EntityTypeManager
+   * @var EntityTypeManager
    */
-  protected $entity_type_manager;
+  protected $entityTypeManager;
 
   /**
    * Drupal\Core\Entity\EntityTypeRepository definition.
    *
-   * @var Drupal\Core\Entity\EntityTypeRepository
+   * @var EntityTypeRepository
    */
-  protected $entity_type_repository;
+  protected $entityTypeRepository;
+
+  /**
+   * Drupal\Core\Entity\Query\QueryFactory definition.
+   *
+   * @var \Drupal\Core\Entity\Query\QueryFactory
+   */
+  protected $entityQuery;
+
+
   /**
    * Constructor.
+   * @param EntityTypeManager $entityTypeManager
+   * @param EntityTypeRepository $entityTypeRepository
+   * @param QueryFactory $entityQuery
    */
-  public function __construct(EntityTypeManager $entity_type_manager, EntityTypeRepository $entity_type_repository) {
-    $this->entity_type_manager = $entity_type_manager;
-    $this->entity_type_repository = $entity_type_repository;
+  public function __construct(EntityTypeManager $entityTypeManager, EntityTypeRepository $entityTypeRepository, QueryFactory $entityQuery) {
+    $this->entityTypeManager = $entityTypeManager;
+    $this->entityTypeRepository = $entityTypeRepository;
+    $this->entityQuery = $entityQuery;
   }
 
   /**
    * Returns a loaded HeartbeatStream entity
    * @param $id
    * @return \Drupal\Core\Entity\EntityInterface|null
+   * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
    */
   public function getEntityById($id) {
-    return $this->entity_type_manager->getStorage('heartbeat_stream')->load($id);
+    return $this->entityTypeManager->getStorage('heartbeat_stream')->load($id);
   }
 
 
@@ -48,9 +63,21 @@ class HeartbeatStreamServices {
    * HeartbeatStream specified by ID
    * @param $id
    * @return mixed
+   * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
    */
   public function getTypesById($id) {
-    return $this->entity_type_manager->getStorage('heartbeat_stream')->load($id)->get('types');
+    return $this->entityTypeManager->getStorage('heartbeat_stream')->load($id)->get('types');
+  }
+
+  /**
+   * Returns an array of HeartbeatStream entities
+   * HeartbeatStream specified by ID
+   * @return mixed
+   */
+  public function loadAllEntities() {
+    $jigga = $this->entityQuery->get('heartbeat_stream');
+    $jiggais =' nada';
+    return $jigga;
   }