RouteSubscriber.php in Printer and PDF versions for Drupal 8+ 8
File
src/Routing/RouteSubscriber.php
View source
<?php
namespace Drupal\printable\Routing;
use Drupal\Core\Routing\RoutingEvents;
use Drupal\Core\Routing\RouteBuildEvent;
use Drupal\printable\PrintableEntityManagerInterface;
use Symfony\Component\Routing\Route;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class RouteSubscriber implements EventSubscriberInterface {
protected $printableEntityManager;
public function __construct(PrintableEntityManagerInterface $printable_entity_manager) {
$this->printableEntityManager = $printable_entity_manager;
}
public static function getSubscribedEvents() {
$events[RoutingEvents::ALTER] = 'routes';
return $events;
}
public function routes(RouteBuildEvent $event) {
$collection = $event
->getRouteCollection();
foreach ($this->printableEntityManager
->getPrintableEntities() as $entity_type => $entity_definition) {
$route = new Route("{$entity_type}/{entity}/printable/{printable_format}", [
'_controller' => 'Drupal\\printable\\Controller\\PrintableController::showFormat',
'_title' => 'Printable',
], [
'_entity_access' => 'entity.view',
'_permission' => 'view printer friendly versions',
], [
'parameters' => [
'entity' => [
'type' => 'entity:' . $entity_type,
],
],
]);
$collection
->add('printable.show_format.' . $entity_type, $route);
}
}
}
Classes
Name |
Description |
RouteSubscriber |
Defines a route subscriber to generate print route for all content entities. |