You are here

class RouteSubscriber in Printer and PDF versions for Drupal 8+ 8

Same name and namespace in other branches
  1. 2.x src/Routing/RouteSubscriber.php \Drupal\printable\Routing\RouteSubscriber

Defines a route subscriber to generate print route for all content entities.

Hierarchy

  • class \Drupal\printable\Routing\RouteSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of RouteSubscriber

1 string reference to 'RouteSubscriber'
printable.services.yml in ./printable.services.yml
printable.services.yml
1 service uses RouteSubscriber
printable.subscriber.route in ./printable.services.yml
Drupal\printable\Routing\RouteSubscriber

File

src/Routing/RouteSubscriber.php, line 14

Namespace

Drupal\printable\Routing
View source
class RouteSubscriber implements EventSubscriberInterface {

  /**
   * The printable entity manager service.
   *
   * @var \Drupal\printable\PrintableEntityManagerInterface
   */
  protected $printableEntityManager;

  /**
   * Constructs a printable RouteSubscriber object.
   *
   * @param \Drupal\printable\PrintableEntityManagerInterface $printable_entity_manager
   *   The printable entity manager service.
   */
  public function __construct(PrintableEntityManagerInterface $printable_entity_manager) {
    $this->printableEntityManager = $printable_entity_manager;
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    $events[RoutingEvents::ALTER] = 'routes';
    return $events;
  }

  /**
   * Adds a print route for each content entity.
   *
   * @param \Drupal\Core\Routing\RouteBuildEvent $event
   *   The route build event.
   */
  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);
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
RouteSubscriber::$printableEntityManager protected property The printable entity manager service.
RouteSubscriber::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
RouteSubscriber::routes public function Adds a print route for each content entity.
RouteSubscriber::__construct public function Constructs a printable RouteSubscriber object.