ParamConverterManager::convert

public ParamConverterManager::convert(array $defaults)

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.
    $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;
}
doc_Drupal
2016-10-29 09:33:08
Comments
Leave a Comment

Please login to continue.