You are here

class RouteSubscriber in Workspace 8

Subscriber for Workspace routes.

Hierarchy

Expanded class hierarchy of RouteSubscriber

1 string reference to 'RouteSubscriber'
workspace.services.yml in ./workspace.services.yml
workspace.services.yml
1 service uses RouteSubscriber
workspace.route_subscriber in ./workspace.services.yml
Drupal\workspace\Routing\RouteSubscriber

File

src/Routing/RouteSubscriber.php, line 14

Namespace

Drupal\workspace\Routing
View source
class RouteSubscriber extends RouteSubscriberBase {

  /**
   * @var \Drupal\multiversion\MultiversionManagerInterface
   */
  protected $multiversionManager;

  /**
   * Constructs a new RouteSubscriber object.
   *
   * @param \Drupal\multiversion\MultiversionManagerInterface $multiversion_manager
   */
  public function __construct(MultiversionManagerInterface $multiversion_manager) {
    $this->multiversionManager = $multiversion_manager;
  }

  /**
   * {@inheritdoc}
   */
  protected function alterRoutes(RouteCollection $collection) {
    foreach ($this->multiversionManager
      ->getEnabledEntityTypes() as $entity_type_id => $entity_type) {
      if ($entity_type
        ->hasLinkTemplate('version-tree')) {
        $options = [
          '_admin_route' => TRUE,
          '_entity_type_id' => $entity_type_id,
          'parameters' => [
            $entity_type_id => [
              'type' => 'entity:' . $entity_type_id,
            ],
          ],
        ];
        if ($link_template = $entity_type
          ->getLinkTemplate('version-tree')) {
          $route = new Route($link_template, [
            '_controller' => '\\Drupal\\workspace\\Controller\\RevisionsController::revisions',
            '_title' => 'Revisions',
          ], [
            '_permission' => 'view_revision_trees',
          ], $options);

          // This will create new routes
          $collection
            ->add("entity.{$entity_type_id}.version_tree", $route);
        }
        if (($link_template = $entity_type
          ->getLinkTemplate('revision')) && empty($collection
          ->get("entity.{$entity_type_id}.revision"))) {
          unset($options['_admin_route']);
          $options['parameters']['entity_revision'] = [
            'type' => 'entity_revision:' . $entity_type_id,
          ];
          $route = new Route($link_template, [
            '_controller' => '\\Drupal\\workspace\\Controller\\RevisionController::view',
            '_title_callback' => '\\Drupal\\workspace\\Controller\\RevisionController::viewTitle',
          ], [
            '_permission' => 'view_revision_trees',
          ], $options);

          // This will create new routes (and override the revision
          // route for entity types that already has one).
          $collection
            ->add("entity.{$entity_type_id}.revision", $route);
        }
      }
    }
  }

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

}

Members

Namesort descending Modifiers Type Description Overrides
RouteSubscriber::$multiversionManager protected property
RouteSubscriber::alterRoutes protected function Alters existing routes for a specific collection. Overrides RouteSubscriberBase::alterRoutes
RouteSubscriber::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to. Overrides RouteSubscriberBase::getSubscribedEvents
RouteSubscriber::__construct public function Constructs a new RouteSubscriber object.
RouteSubscriberBase::onAlterRoutes public function Delegates the route altering to self::alterRoutes(). 1