You are here

protected function RouteSubscriber::alterRoutes in Transaction 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 36

Class

RouteSubscriber
Subscriber for transaction routes.

Namespace

Drupal\transaction\Routing

Code

protected function alterRoutes(RouteCollection $collection) {

  // Add transaction collection route for transaction types with a link
  // template for it.
  foreach ($this->entityTypeManager
    ->getStorage('transaction_type')
    ->loadMultiple() as $transaction_type_id => $transaction_type) {

    /** @var \Drupal\transaction\TransactionTypeInterface $transaction_type */
    if ($route_path = $this->entityTypeManager
      ->getDefinition($target_entity_type_id = $transaction_type
      ->getTargetEntityTypeId())
      ->getLinkTemplate("transaction-{$transaction_type_id}")) {
      $route = new Route($route_path);
      $route
        ->addDefaults([
        '_entity_list' => 'transaction',
        '_title' => 'Transactions',
        '_title_callback' => '\\Drupal\\transaction\\Controller\\TransactionController::transactionCollectionTitle',
      ])
        ->addRequirements([
        '_permission' => "view any {$transaction_type_id} transaction",
        '_entity_access' => "{$target_entity_type_id}.view",
        $target_entity_type_id => '\\d+',
        '_applicable_transaction_type' => 'TRUE',
      ])
        ->setOption('_admin_route', TRUE)
        ->setOption('_transaction_transaction_type_id', $transaction_type_id)
        ->setOption('_transaction_target_entity_type_id', $target_entity_type_id)
        ->setOption('parameters', [
        $target_entity_type_id => [
          'type' => 'entity:' . $target_entity_type_id,
        ],
        'transaction_type' => [
          'type' => 'entity:transaction_type',
        ],
      ]);
      $collection
        ->add("entity.{$target_entity_type_id}.{$transaction_type_id}-transaction", $route);

      // Transaction creation route variant. The name of the target entity
      // parameter must match the target entity id.
      $route = clone $collection
        ->get('entity.transaction.add_form');
      $route
        ->setPath("/transaction/add/{transaction_type}/{target_entity_type}/{{$target_entity_type_id}}")
        ->setOption('_transaction_target_entity_type_id', $target_entity_type_id);
      $route_options = $route
        ->getOptions();
      unset($route_options['parameters']['target_entity']);
      $route_options['parameters'][$target_entity_type_id]['type'] = 'entity:' . $target_entity_type_id;
      $route
        ->setOptions($route_options);
      $route_requirements = $route
        ->getRequirements();
      $route_requirements['_entity_access'] = $target_entity_type_id . '.view';
      $route_requirements[$target_entity_type_id] = '\\d+';
      unset($route_requirements['target_entity']);
      $route
        ->setRequirements($route_requirements);
      $collection
        ->add("entity.transaction.{$target_entity_type_id}.{$transaction_type_id}.add_form", $route);
    }
  }
}