Преглед изворни кода

Dynamic routing is working:
Routes are created for all HeartbeatStreams using their respective path values

logicp пре 7 година
родитељ
комит
153a4d9999

+ 2 - 2
heartbeat.install

@@ -302,11 +302,11 @@ function heartbeat_uninstall() {
 //    $heartbeat = \Drupal::service("entity_type.manager")->getStorage("heartbeat")->load($entity);
 //    $heartbeat->delete();
 //  }
-//
+
 //  $entities = \Drupal::service("entity.query")->get("heartbeat_stream")->execute();
 //
 //  foreach($entities as $entity) {
-//    $heartbeat = \Drupal::service("entity_type.manager")->getStorage("heartbeat")->load($entity);
+//    $heartbeat = \Drupal::service("entity_type.manager")->getStorage("heartbeat_stream")->load($entity);
 //    $heartbeat->delete();
 //  }
 }

+ 40 - 29
heartbeat.routing.yml

@@ -16,34 +16,45 @@ heartbeat.test_controller_start:
   requirements:
     _permission: 'access content'
 
-heartbeat.stream_test_controller_stream:
-  path: '/nodeactivity'
-  defaults:
-    _controller: '\Drupal\heartbeat\Controller\StreamTestController::stream'
-    _title: 'stream'
-  requirements:
-    _permission: 'access content'
+#heartbeat.stream_test_controller_stream:
+#  path: '/nodeactivity'
+#  defaults:
+#    _controller: '\Drupal\heartbeat\Controller\StreamTestController::stream'
+#    _title: 'stream'
+#  requirements:
+#    _permission: 'access content'
+#
+#heartbeat.stream_test_controller_friendstream:
+#  path: '/friendactivity'
+#  defaults:
+#    _controller: '\Drupal\heartbeat\Controller\StreamTestController::friendstream'
+#    _title: 'Friendship'
+#  requirements:
+#    _permission: 'access content'
+#
+#heartbeat.stream_test_controller_tweetstream:
+#  path: '/tweetactivity'
+#  defaults:
+#    _controller: '\Drupal\heartbeat\Controller\StreamTestController::tweetstream'
+#    _title: 'Tweetship'
+#  requirements:
+#    _permission: 'access content'
+#
+#heartbeat.stream_test_controller_superherostream:
+#  path: '/superheroactivity'
+#  defaults:
+#    _controller: '\Drupal\heartbeat\Controller\StreamTestController::superherostream'
+#    _title: 'Superhero Worship'
+#  requirements:
+#    _permission: 'access content'
 
-heartbeat.stream_test_controller_friendstream:
-  path: '/friendactivity'
-  defaults:
-    _controller: '\Drupal\heartbeat\Controller\StreamTestController::friendstream'
-    _title: 'Friendship'
-  requirements:
-    _permission: 'access content'
+route_callbacks:
+  - '\Drupal\heartbeat\Routing\HeartbeatRouteController::getRoutes'
 
-heartbeat.stream_test_controller_tweetstream:
-  path: '/tweetactivity'
-  defaults:
-    _controller: '\Drupal\heartbeat\Controller\StreamTestController::tweetstream'
-    _title: 'Tweetship'
-  requirements:
-    _permission: 'access content'
-
-heartbeat.stream_test_controller_superherostream:
-  path: '/superheroactivity'
-  defaults:
-    _controller: '\Drupal\heartbeat\Controller\StreamTestController::superherostream'
-    _title: 'Superhero Worship'
-  requirements:
-    _permission: 'access content'
+#heartbeat.heartbeat_stream.routes:
+#  path: '/heartbeat/hello'
+#  defaults:
+#    _controller: '\Drupal\heartbeat\Controller\HeartbeatRouteController::getRoutes'
+#    _title: 'getRoutes'
+#  requirements:
+#    _permission: 'access content'

+ 85 - 2
src/Controller/HeartbeatStreamController.php

@@ -4,7 +4,11 @@ namespace Drupal\heartbeat\Controller;
 
 use Drupal\Component\Utility\Xss;
 use Drupal\Core\Controller\ControllerBase;
-use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
+use Drupal\Core\Entity\Query\QueryFactory;
+use Drupal\Core\Entity\EntityTypeManager;
+use Drupal\heartbeat\HeartbeatService;
+use Drupal\heartbeat\HeartbeatStreamServices;
+use Symfony\Component\DependencyInjection\ContainerInterface;
 use Drupal\Core\Url;
 use Drupal\heartbeat\Entity\HeartbeatStreamInterface;
 
@@ -15,7 +19,86 @@ use Drupal\heartbeat\Entity\HeartbeatStreamInterface;
  *
  * @package Drupal\heartbeat\Controller
  */
-class HeartbeatStreamController extends ControllerBase implements ContainerInjectionInterface {
+class HeartbeatStreamController extends ControllerBase {
+
+  /**
+   * Drupal\Core\Entity\Query\QueryFactory definition.
+   *
+   * @var \Drupal\Core\Entity\Query\QueryFactory
+   */
+  protected $entityQuery;
+  /**
+   * Drupal\Core\Entity\EntityTypeManager definition.
+   *
+   * @var \Drupal\Core\Entity\EntityTypeManager
+   */
+  protected $entityTypeManager;
+
+  /**
+   * \Drupal\heartbeat\HeartbeatStreamServices definition.
+   *
+   * @var \Drupal\heartbeat\HeartbeatStreamServices
+   */
+  protected $heartbeatStreamService;
+
+  /**
+   * \Drupal\heartbeat\HeartbeatService definition.
+   *
+   * @var \Drupal\heartbeat\HeartbeatService
+   */
+  protected $heartbeatService;
+
+  /**
+   * {@inheritdoc}
+   */
+  public function __construct(QueryFactory $entity_query, EntityTypeManager $entity_type_manager, HeartbeatStreamServices $heartbeatStreamService, HeartbeatService $heartbeatService) {
+    $this->entityQuery = $entity_query;
+    $this->entityTypeManager = $entity_type_manager;
+    $this->heartbeatStreamService = $heartbeatStreamService;
+    $this->heartbeatService = $heartbeatService;
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public static function create(ContainerInterface $container) {
+    return new static(
+      $container->get('entity.query'),
+      $container->get('entity_type.manager'),
+      $container->get('heartbeatstream'),
+      $container->get('heartbeat')
+    );
+  }
+
+  /**
+   * Getroutes.
+   *
+   * @return string
+   *   Return Hello string.
+   */
+  public function createRoute($heartbeatStreamId) {
+
+    $messages = array();
+    $types = $this->heartbeatStreamService->getTypesById($heartbeatStreamId);
+    foreach ($types as $type) {
+      if ($type != null) {
+
+        $heartbeatType = $type->getValue();
+
+        $heartbeats = $this->heartbeatService->loadByType($heartbeatType);
+
+        foreach($heartbeats as $heartbeat) {
+          $messages[] = $heartbeat->getMessage()->getValue()[0]['value'];
+        }
+      }
+    }
+    return [
+      '#theme' => 'heartbeat_stream',
+      '#messages' => array_reverse($messages),
+      '#attached' => array('library' => 'heartbeat/heartbeat')
+    ];
+
+  }
 
   /**
    * Displays a Heartbeat stream  revision.

+ 18 - 28
src/Controller/TestController.php

@@ -6,6 +6,9 @@ use Drupal\Core\Controller\ControllerBase;
 use Symfony\Component\DependencyInjection\ContainerInterface;
 use Drupal\heartbeat\HeartbeatTypeServices;
 use Drupal\heartbeat\HeartbeatStreamServices;
+use Symfony\Component\Routing\Route;
+use Symfony\Component\Routing\RouteCollection;
+
 
 /**
  * Class TestController.
@@ -26,13 +29,13 @@ class TestController extends ControllerBase {
    *
    * @var HeartbeatStreamServices
    */
-  protected $heartbeatstream;
+  protected $heartbeatStream;
   /**
    * {@inheritdoc}
    */
   public function __construct(HeartbeatTypeServices $heartbeat_heartbeattype, HeartbeatStreamServices $heartbeatstream) {
     $this->heartbeat_heartbeattype = $heartbeat_heartbeattype;
-    $this->heartbeatstream = $heartbeatstream;
+    $this->heartbeatStream = $heartbeatstream;
   }
 
   /**
@@ -42,6 +45,7 @@ class TestController extends ControllerBase {
     return new static(
       $container->get('heartbeat.heartbeattype'),
       $container->get('heartbeatstream')
+
     );
   }
 
@@ -52,36 +56,22 @@ class TestController extends ControllerBase {
    *   Return Hello string.
    */
   public function start($arg) {
+    foreach ($this->heartbeatStream->getAllStreams() as $heartbeatStream) {
 
-    $node = \Drupal\node\Entity\Node::load(186);
-
-    $streamEntities = $this->heartbeatstream->loadAllEntities();
-
-    foreach ($streamEntities as $streamEntityId) {
-
-      $streamEntity = $this->heartbeatstream->getEntityById($streamEntityId);
-      $types = $streamEntity->get('types');
-      $arg .= 'Stream::   ' . $streamEntity->id();
+      $route = new Route(
+        $heartbeatStream->getPath()->getValue()[0]['value'],
 
-      $i = 1;
+        array(
+          '_controller' => '\Drupal\heartbeat\Controller\HeartbeatStreamController::createRoute',
+          '_title' => $heartbeatStream->getName(),
+          'heartbeatStreamId' => $heartbeatStream->id(),
+        ),
+        array(
+          '_permission'  => 'access content',
+        )
+      );
 
-      foreach ($types->getValue() as $heartbeatType) {
-        $arg .= '   ' . $i . '. ' . $heartbeatType['target_id'];
-        $i++;
-      }
     }
-
-    $heartbeatTypeService = \Drupal::service('heartbeat.heartbeattype');
-
-    $entityBundleInfo = $node->bundle();
-    $entityType = $node->getEntityType();
-    $availableBundles = $heartbeatTypeService->getEntityBundles($node->getEntityType());
-
-    foreach ($heartbeatTypeService->getTypes() as $type) {
-      $heartbeatTypeEntity = \Drupal::entityTypeManager()->getStorage('heartbeat_type')->load($type);
-    }
-    $emptyVariable = 'not empty';
-
     return [
       '#type' => 'markup',
       '#markup' => $this->t('Implement method: start with parameter(s): ' . $arg),

+ 2 - 3
src/Form/HeartbeatStreamForm.php

@@ -74,9 +74,8 @@ class HeartbeatStreamForm extends ContentEntityForm {
   public function buildForm(array $form, FormStateInterface $form_state) {
     /* @var $entity \Drupal\heartbeat\Entity\HeartbeatStream */
 
-    if ($entity == NULL) {
-      $entity = &$this->entity;
-    }
+    $entity = &$this->entity;
+
     $form = parent::buildForm($form, $form_state);
 
     if (!$this->entity->isNew()) {

+ 7 - 0
src/HeartbeatStreamServices.php

@@ -79,4 +79,11 @@ class HeartbeatStreamServices {
   }
 
 
+  /*
+   * Load all available HeartbeatStream entities
+   */
+  public function getAllStreams() {
+    return $this->entityTypeManager->getStorage('heartbeat_stream')->loadMultiple($this->loadAllEntities());
+  }
+
 }

+ 100 - 0
src/Routing/HeartbeatRouteController.php

@@ -0,0 +1,100 @@
+<?php
+
+namespace Drupal\heartbeat\Routing;
+
+use Drupal\Core\Controller\ControllerBase;
+use Symfony\Component\DependencyInjection\ContainerInterface;
+use Symfony\Component\Routing\Route;
+use Symfony\Component\Routing\RouteCollection;
+use Drupal\heartbeat\HeartbeatStream;
+use Drupal\heartbeat\HeartbeatService;
+use Drupal\heartbeat\HeartbeatStreamServices;
+use Drupal\Core\Entity\Query\QueryFactory;
+use Drupal\Core\Entity\EntityTypeManager;
+
+/**
+ * Class HeartbeatRouteController.
+ *
+ * @package Drupal\heartbeat\Controller
+ */
+class HeartbeatRouteController extends ControllerBase {
+
+  /**
+   * Drupal\heartbeat\HeartbeatService definition.
+   *
+   * @var \Drupal\heartbeat\HeartbeatService
+   */
+  protected $heartbeatService;
+  /**
+   * Drupal\heartbeat\HeartbeatStreamServices definition.
+   *
+   * @var \Drupal\heartbeat\HeartbeatStreamServices
+   */
+  protected $heartbeatStreamService;
+  /**
+   * Drupal\Core\Entity\Query\QueryFactory definition.
+   *
+   * @var \Drupal\Core\Entity\Query\QueryFactory
+   */
+  protected $entityQuery;
+  /**
+   * Drupal\Core\Entity\EntityTypeManager definition.
+   *
+   * @var \Drupal\Core\Entity\EntityTypeManager
+   */
+  protected $entityTypeManager;
+
+  /**
+   * {@inheritdoc}
+   */
+  public function __construct(HeartbeatService $heartbeatService, HeartbeatStreamServices $heartbeatStreamService, QueryFactory $entityQuery, EntityTypeManager $entityTypeManager) {
+    $this->heartbeatService = $heartbeatService;
+    $this->heartbeatStreamService = $heartbeatStreamService;
+    $this->entityQuery = $entityQuery;
+    $this->entityTypeManager = $entityTypeManager;
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public static function create(ContainerInterface $container) {
+    return new static(
+      $container->get('heartbeat'),
+      $container->get('heartbeatstream'),
+      $container->get('entity.query'),
+      $container->get('entity_type.manager')
+    );
+  }
+
+  /**
+   * Getroutes.
+   *
+   * @return string
+   *   Return Hello string.
+   */
+  public function getRoutes() {
+    \Drupal::logger('HeartbeatRouteController')->debug('We getting called, yo');
+
+    $routeCollection = new RouteCollection();
+
+    foreach ($this->heartbeatStreamService->getAllStreams() as $heartbeatStream) {
+      $route = new Route(
+        $heartbeatStream->getPath()->getValue()[0]['value'],
+
+        array(
+          '_controller' => '\Drupal\heartbeat\Controller\HeartbeatStreamController::createRoute',
+          '_title' => $heartbeatStream->getName(),
+          'heartbeatStreamId' => $heartbeatStream->id(),
+        ),
+        array(
+          '_permission'  => 'access content',
+        )
+      );
+      // Add the route under the name 'example.content'.
+      $routeCollection->add('heartbeat.' . $heartbeatStream->getName(), $route);
+    }
+    \Drupal::logger('HeartbeatRouteController')->debug('Data is %data', array('%data' => $routeCollection));
+    return $routeCollection;
+
+  }
+}