You are here

protected function RouteSubscriber::alterRoutes in State Machine 8

Alters existing routes for a specific collection.

Parameters

\Symfony\Component\Routing\RouteCollection $collection: The route collection for adding routes.

Overrides RouteSubscriberBase::alterRoutes

File

src/Routing/RouteSubscriber.php, line 35

Class

RouteSubscriber
Subscriber for state machine transition routes on entities.

Namespace

Drupal\state_machine\Routing

Code

protected function alterRoutes(RouteCollection $collection) {
  foreach ($this->entityTypeManager
    ->getDefinitions() as $entity_type_id => $entity_type) {
    if (!$entity_type
      ->hasLinkTemplate('state-transition-form')) {
      continue;
    }
    $route = new Route($entity_type
      ->getLinkTemplate('state-transition-form'));
    $route
      ->setDefaults([
      '_entity_form' => "{$entity_type_id}.state-transition-confirm",
    ])
      ->setRequirement('_state_transition_access', "TRUE")
      ->setRequirement($entity_type_id, '\\d+')
      ->setRequirement('transition_id', '[a-z_]+')
      ->setRequirement('field_name', '[a-z_]+')
      ->setOption('parameters', [
      $entity_type_id => [
        'type' => 'entity:' . $entity_type_id,
      ],
    ]);
    $collection
      ->add("entity.{$entity_type_id}.state_transition_form", $route);
  }
}