certificate-4.0.0-alpha1/src/Routing/CertificateRoutes.php
src/Routing/CertificateRoutes.php
<?php
namespace Drupal\certificate\Routing;
use Symfony\Component\Routing\Route;
use function certificate_get_entity_types;
/**
* Defines dynamic routes.
*/
class CertificateRoutes {
/**
* {@inheritdoc}
*/
public function routes() {
$routes = [];
$etm = \Drupal::entityTypeManager();
foreach (certificate_get_entity_types() as $entity_type) {
// Add a certificate route based on the canonical URL.
$template = $etm->getStorage($entity_type)->getEntityType()->getLinkTemplate('canonical');
$tab_route = new Route($template . '/certificate');
$tab_route->setDefault('_controller', '\Drupal\certificate\Controller\CertificateController::certificatePage');
$tab_route->setDefault('_title', 'Certificate');
$tab_route->setOption('parameters', [
$entity_type => [
'type' => "entity:$entity_type",
],
]);
$tab_route->setRequirements(['_custom_access' => '\Drupal\certificate\Controller\CertificateController::accessTab']);
$routes["certificate.$entity_type"] = $tab_route;
$user_tab_route = clone $tab_route;
$user_tab_route->setPath($template . '/certificate/{user}');
$user_tab_route->setOption('parameters', [
$entity_type => [
'type' => "entity:$entity_type",
],
'user' => [
'type' => 'entity:user',
],
]);
$routes["certificate.$entity_type.user"] = $user_tab_route;
$pdf_route = new Route("$entity_type/{{$entity_type}}/certificate/{user}/{certificate_template}/pdf");
$pdf_route->setDefault('_controller', '\Drupal\certificate\Controller\CertificateController::certificateDownload');
$pdf_route->setDefault('_title', 'Download');
$pdf_route->setOption('params', [
'user' => 'type: entity:user',
'certificate_template' => 'type: entity:certificate_template',
]);
$pdf_route->setRequirements(['_custom_access' => '\Drupal\certificate\Controller\CertificateController::accessPdf']);
$routes["certificate.$entity_type.pdf"] = $pdf_route;
}
return $routes;
}
}
