diff --git a/modules/custom/dkan_frontend/dkan_frontend.services.yml b/modules/custom/dkan_frontend/dkan_frontend.services.yml index 3208caeec..24788c3e1 100644 --- a/modules/custom/dkan_frontend/dkan_frontend.services.yml +++ b/modules/custom/dkan_frontend/dkan_frontend.services.yml @@ -8,3 +8,4 @@ services: class: Drupal\dkan_frontend\Routing\RouteProvider arguments: - '@app.root' + - '@entity.query' diff --git a/modules/custom/dkan_frontend/src/Controller/Page.php b/modules/custom/dkan_frontend/src/Controller/Page.php index 1c1fe6b50..ea219296f 100644 --- a/modules/custom/dkan_frontend/src/Controller/Page.php +++ b/modules/custom/dkan_frontend/src/Controller/Page.php @@ -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; /** @@ -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); } diff --git a/modules/custom/dkan_frontend/src/Routing/RouteProvider.php b/modules/custom/dkan_frontend/src/Routing/RouteProvider.php index bb1cce163..0f06becab 100644 --- a/modules/custom/dkan_frontend/src/Routing/RouteProvider.php +++ b/modules/custom/dkan_frontend/src/Routing/RouteProvider.php @@ -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; @@ -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; } /** @@ -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']); @@ -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)); + } + } + } diff --git a/modules/custom/dkan_frontend/tests/src/Unit/Routing/RouteProvider2Test.php b/modules/custom/dkan_frontend/tests/src/Unit/Routing/RouteProvider2Test.php index d6ba7b19e..997fe3aa3 100644 --- a/modules/custom/dkan_frontend/tests/src/Unit/Routing/RouteProvider2Test.php +++ b/modules/custom/dkan_frontend/tests/src/Unit/Routing/RouteProvider2Test.php @@ -1,9 +1,10 @@ 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();