You are here

public function ParamConverterManager::setRouteParameterConverters in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/lib/Drupal/Core/ParamConverter/ParamConverterManager.php \Drupal\Core\ParamConverter\ParamConverterManager::setRouteParameterConverters()

Saves a list of applicable converters to each route.

Parameters

\Symfony\Component\Routing\RouteCollection $routes: A collection of routes to apply converters to.

Overrides ParamConverterManagerInterface::setRouteParameterConverters

File

core/lib/Drupal/Core/ParamConverter/ParamConverterManager.php, line 51
Contains \Drupal\Core\ParamConverter\ParamConverterManager.

Class

ParamConverterManager
Manages converter services for converting request parameters to full objects.

Namespace

Drupal\Core\ParamConverter

Code

public function setRouteParameterConverters(RouteCollection $routes) {
  foreach ($routes
    ->all() as $route) {
    if (!($parameters = $route
      ->getOption('parameters'))) {

      // Continue with the next route if no parameters have been defined.
      continue;
    }

    // Loop over all defined parameters and look up the right converter.
    foreach ($parameters as $name => &$definition) {
      if (isset($definition['converter'])) {

        // Skip parameters that already have a manually set converter.
        continue;
      }
      foreach (array_keys($this->converters) as $converter) {
        if ($this
          ->getConverter($converter)
          ->applies($definition, $name, $route)) {
          $definition['converter'] = $converter;
          break;
        }
      }
    }

    // Override the parameters array.
    $route
      ->setOption('parameters', $parameters);
  }
}