function RouteSubscriber::getRouteParameters

Same name in other branches
  1. 5.x src/Routing/RouteSubscriber.php \Drupal\devel\Routing\RouteSubscriber::getRouteParameters()

Gets the route parameters from the template.

Parameters

\Drupal\Core\Entity\EntityTypeInterface $entity_type: The entity type definition.

string $link_template: The link template.

Return value

array[] A list of route of parameters.

3 calls to RouteSubscriber::getRouteParameters()
RouteSubscriber::getEntityLoadRoute in src/Routing/RouteSubscriber.php
Gets the entity load route.
RouteSubscriber::getEntityRenderRoute in src/Routing/RouteSubscriber.php
Gets the entity render route.
RouteSubscriber::getEntityTypeDefinitionRoute in src/Routing/RouteSubscriber.php
Gets the entity type definition route.

File

src/Routing/RouteSubscriber.php, line 169

Class

RouteSubscriber
Subscriber for Devel routes.

Namespace

Drupal\devel\Routing

Code

protected function getRouteParameters(EntityTypeInterface $entity_type, string $link_template) : array {
    $parameters = [];
    if (!($path = $entity_type->getLinkTemplate($link_template))) {
        return $parameters;
    }
    $original_route_parameters = [];
    $candidate_routes = $this->routeProvider
        ->getRoutesByPattern($path);
    if ($candidate_routes->count()) {
        // Guess the best match. There could be more than one route sharing the
        // same path. Try first an educated guess based on the route name. If we
        // can't find one, pick-up the first from the list.
        $name = 'entity.' . $entity_type->id() . '.' . str_replace('-', '_', $link_template);
        if (!($original_route = $candidate_routes->get($name))) {
            $iterator = $candidate_routes->getIterator();
            $iterator->rewind();
            $original_route = $iterator->current();
        }
        $original_route_parameters = $original_route->getOption('parameters') ?? [];
    }
    if (preg_match_all('/{\\w*}/', $path, $matches)) {
        foreach ($matches[0] as $match) {
            $match = str_replace([
                '{',
                '}',
            ], '', $match);
            // This match has an original route parameter definition.
            if (isset($original_route_parameters[$match])) {
                $parameters[$match] = $original_route_parameters[$match];
            }
            elseif ($this->entityTypeManager
                ->hasDefinition($match)) {
                $parameters[$match] = [
                    'type' => "entity:{$match}",
                ];
            }
        }
    }
    return $parameters;
}