You are here

class RouteSubscriber in State Machine 8

Subscriber for state machine transition routes on entities.

Hierarchy

Expanded class hierarchy of RouteSubscriber

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

File

src/Routing/RouteSubscriber.php, line 13

Namespace

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

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * Constructs a new RouteSubscriber object.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_manager
   *   The entity type manager.
   */
  public function __construct(EntityTypeManagerInterface $entity_manager) {
    $this->entityTypeManager = $entity_manager;
  }

  /**
   * {@inheritdoc}
   */
  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);
    }
  }

}

Members

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