You are here

public function ParamConverterManager::convert in Drupal 8

Same name and namespace in other branches
  1. 9 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 76

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.
    $value = $defaults[$name];
    $defaults[$name] = $this
      ->getConverter($definition['converter'])
      ->convert($value, $definition, $name, $defaults);
    if (!isset($defaults[$name])) {
      $message = 'The "%s" parameter was not converted for the path "%s" (route name: "%s")';
      $route_name = $defaults[RouteObjectInterface::ROUTE_NAME];
      throw new ParamNotConvertedException(sprintf($message, $name, $route
        ->getPath(), $route_name), 0, NULL, $route_name, [
        $name => $value,
      ]);
    }
  }
  return $defaults;
}