function ParamConverterManager::convert

Same name in other branches
  1. 9 core/lib/Drupal/Core/ParamConverter/ParamConverterManager.php \Drupal\Core\ParamConverter\ParamConverterManager::convert()
  2. 8.9.x core/lib/Drupal/Core/ParamConverter/ParamConverterManager.php \Drupal\Core\ParamConverter\ParamConverterManager::convert()
  3. 11.x core/lib/Drupal/Core/ParamConverter/ParamConverterManager.php \Drupal\Core\ParamConverter\ParamConverterManager::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 \Symfony\Component\Routing\Route $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;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.