You are here

public function ParamConverterManager::convert 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::convert()

Invokes the registered converter for each defined parameter on a route.

Parameters

array $defaults: The route defaults array.

Return value

array The modified defaults.

Throws

\Drupal\Core\ParamConverter\ParamNotConvertedException If one of the assigned converters returned NULL because the given variable could not be converted.

Overrides ParamConverterManagerInterface::convert

File

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

Class

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

Namespace

Drupal\Core\ParamConverter

Code

public function convert(array $defaults) {

  /** @var $route \Symfony\Component\Routing\Route */
  $route = $defaults[RouteObjectInterface::ROUTE_OBJECT];

  // Skip this enhancer if there are no parameter definitions.
  if (!($parameters = $route
    ->getOption('parameters'))) {
    return $defaults;
  }

  // Invoke the registered converter for each parameter.
  foreach ($parameters as $name => $definition) {
    if (!isset($defaults[$name])) {

      // Do not try to convert anything that is already set to NULL.
      continue;
    }
    if (!isset($definition['converter'])) {

      // Continue if no converter has been specified.
      continue;
    }

    // If a converter returns NULL it means that the parameter could not be
    // converted.
    $defaults[$name] = $this
      ->getConverter($definition['converter'])
      ->convert($defaults[$name], $definition, $name, $defaults);
    if (!isset($defaults[$name])) {
      throw new ParamNotConvertedException(sprintf('The "%s" parameter was not converted for the path "%s" (route name: "%s")', $name, $route
        ->getPath(), $defaults[RouteObjectInterface::ROUTE_NAME]));
    }
  }
  return $defaults;
}