Skip to content

Commit

Permalink
Add all dataset pages in dkan_frontend. (#270)
Browse files Browse the repository at this point in the history
  • Loading branch information
fmizzell authored Jan 3, 2020
1 parent ef0cd62 commit 376fed9
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 27 deletions.
1 change: 1 addition & 0 deletions modules/custom/dkan_frontend/dkan_frontend.services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ services:
class: Drupal\dkan_frontend\Routing\RouteProvider
arguments:
- '@app.root'
- '@entity.query'
3 changes: 1 addition & 2 deletions modules/custom/dkan_frontend/src/Controller/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use Drupal\dkan_frontend\Page as PageBuilder;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;

/**
Expand Down Expand Up @@ -37,7 +36,7 @@ public function __construct(PageBuilder $pageBuilder) {
public function page($name) {
$pageContent = $this->pageBuilder->build($name);
if (empty($pageContent)) {
throw new NotFoundHttpException('Page could not be loaded');
$pageContent = $this->pageBuilder->build("dataset");
}
return Response::create($pageContent);
}
Expand Down
70 changes: 49 additions & 21 deletions modules/custom/dkan_frontend/src/Routing/RouteProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Drupal\dkan_frontend\Routing;

use Drupal\Core\Entity\Query\QueryFactory;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;

Expand All @@ -11,12 +12,14 @@
class RouteProvider {

private $appRoot;
private $entityQuery;

/**
* Constructor.
*/
public function __construct(string $appRoot) {
public function __construct(string $appRoot, QueryFactory $entityQuery) {
$this->appRoot = $appRoot;
$this->entityQuery = $entityQuery;
}

/**
Expand All @@ -25,26 +28,8 @@ public function __construct(string $appRoot) {
public function routes() {
$routes = new RouteCollection();

$base = $this->appRoot . "/data-catalog-frontend/public";
$possible_pages = $this->expandDirectories($base);

foreach ($possible_pages as $possible_page) {
if (file_exists($possible_page . "/index.html")) {
$name = self::getNameFromPath($possible_page);
$path = str_replace($base, "", $possible_page);
$routes->add($name, $this->routeHelper($path, $name));
}
}

$route = new Route(
"/home",
[
'_controller' => '\Drupal\dkan_frontend\Controller\Page::page',
'name' => 'home',
]
);
$route->setMethods(['GET']);
$routes->add('home', $route);
$this->addStaticPages($routes);
$this->addDatasets($routes);

$routes->addRequirements(['_access' => 'TRUE']);

Expand Down Expand Up @@ -101,4 +86,47 @@ private function routeHelper(string $path, string $name) : Route {
return $route;
}

/**
* Private.
*/
private function addStaticPages(RouteCollection $routes) {
$base = $this->appRoot . "/data-catalog-frontend/public";
$possible_pages = $this->expandDirectories($base);

foreach ($possible_pages as $possible_page) {
if (file_exists($possible_page . "/index.html")) {
$name = self::getNameFromPath($possible_page);
$path = str_replace($base, "", $possible_page);
$routes->add($name, $this->routeHelper($path, $name));
}
}

$route = new Route(
"/home",
[
'_controller' => '\Drupal\dkan_frontend\Controller\Page::page',
'name' => 'home',
]
);
$route->setMethods(['GET']);
$routes->add('home', $route);
}

/**
* Private.
*/
private function addDatasets(RouteCollection $routes) {
$query = $this->entityQuery->get("node");
$query->condition('type', 'data');
$query->condition('field_data_type', 'dataset');
$result = $query->execute();

foreach ($result as $item) {
$node = node_load($item);
$uuid = $node->get("uuid")->value;
$name = "dataset__{$uuid}";
$routes->add($name, $this->routeHelper("/dataset/{$uuid}", $name));
}
}

}
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
<?php

/**
*
*/use Drupal\dkan_frontend\Routing\RouteProvider;
use Drupal\Core\Entity\Query\QueryFactory;
use Drupal\dkan_frontend\Routing\RouteProvider;
use MockChain\Chain;
use PHPUnit\Framework\TestCase;
use Drupal\Core\Entity\Query\QueryInterface;

/**
*
Expand All @@ -14,7 +15,14 @@ class RouteProvider2Test extends TestCase {
*
*/
public function test() {
$provider = new RouteProvider(__DIR__ . "/../../../app");

$queryFactory = (new Chain($this))
->add(QueryFactory::class, "get", QueryInterface::class)
->add(QueryInterface::class, 'condition', QueryInterface::class)
->add(QueryInterface::class, 'execute', [])
->getMock();

$provider = new RouteProvider(__DIR__ . "/../../../app", $queryFactory);

/* @var $routes \Symfony\Component\Routing\RouteCollection */
$routes = $provider->routes();
Expand Down

0 comments on commit 376fed9

Please sign in to comment.