class ParamConverterManager

Same name and namespace in other branches
  1. 9 core/lib/Drupal/Core/ParamConverter/ParamConverterManager.php \Drupal\Core\ParamConverter\ParamConverterManager
  2. 8.9.x core/lib/Drupal/Core/ParamConverter/ParamConverterManager.php \Drupal\Core\ParamConverter\ParamConverterManager
  3. 10 core/lib/Drupal/Core/ParamConverter/ParamConverterManager.php \Drupal\Core\ParamConverter\ParamConverterManager

Manages converter services for converting request parameters to full objects.

A typical use case for this would be upcasting (converting) a node id to a node entity.

Hierarchy

Expanded class hierarchy of ParamConverterManager

1 file declares its use of ParamConverterManager
ParamConverterManagerTest.php in core/tests/Drupal/Tests/Core/ParamConverter/ParamConverterManagerTest.php

File

core/lib/Drupal/Core/ParamConverter/ParamConverterManager.php, line 14

Namespace

Drupal\Core\ParamConverter
View source
class ParamConverterManager implements ParamConverterManagerInterface {
    
    /**
     * Array of loaded converter services keyed by their ids.
     *
     * @var array
     */
    protected $converters = [];
    
    /**
     * {@inheritdoc}
     */
    public function addConverter(ParamConverterInterface $param_converter, $id) {
        $this->converters[$id] = $param_converter;
        return $this;
    }
    
    /**
     * {@inheritdoc}
     */
    public function getConverter($converter) {
        if (isset($this->converters[$converter])) {
            return $this->converters[$converter];
        }
        else {
            throw new \InvalidArgumentException(sprintf('No converter has been registered for %s', $converter));
        }
    }
    
    /**
     * {@inheritdoc}
     */
    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);
        }
    }
    
    /**
     * {@inheritdoc}
     */
    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;
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
ParamConverterManager::$converters protected property Array of loaded converter services keyed by their ids.
ParamConverterManager::addConverter public function Registers a parameter converter with the manager. Overrides ParamConverterManagerInterface::addConverter
ParamConverterManager::convert public function Invokes the registered converter for each defined parameter on a route. Overrides ParamConverterManagerInterface::convert
ParamConverterManager::getConverter public function Lazy-loads converter services. Overrides ParamConverterManagerInterface::getConverter
ParamConverterManager::setRouteParameterConverters public function Saves a list of applicable converters to each route. Overrides ParamConverterManagerInterface::setRouteParameterConverters

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