class ParamConverterManager

Provides a service which allows to enhance (say alter) the arguments coming from the URL.

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

This class will not enhance any of the arguments itself, but allow other services to register to do so.

Hierarchy

Expanded class hierarchy of ParamConverterManager

1 string reference to 'ParamConverterManager'
core.services.yml in drupal/core/core.services.yml
drupal/core/core.services.yml

File

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

Namespace

Drupal\Core\ParamConverter
View source
class ParamConverterManager implements RouteEnhancerInterface {

  /**
   * Converters managed by the ParamConverterManager.
   *
   * @var array
   */
  protected $converters;

  /**
   * Adds a converter to the paramconverter service.
   *
   * @see \Drupal\Core\DependencyInjection\Compiler\RegisterParamConvertersPass
   *
   * @param \Drupal\Core\ParamConverter\ParamConverterInterface $converter
   *   The converter to add.
   */
  public function addConverter(ParamConverterInterface $converter) {
    $this->converters[] = $converter;
    return $this;
  }

  /**
   * Implements \Symfony\Cmf\Component\Routing\Enhancer\ŖouteEnhancerIterface.
   *
   * Iterates over all registered converters and allows them to alter the
   * defaults.
   *
   * @param array $defaults
   *   The getRouteDefaults array.
   * @param \Symfony\Component\HttpFoundation\Request $request
   *   The current request.
   *
   * @return array
   *   The modified defaults.
   */
  public function enhance(array $defaults, Request $request) {

    // This array will collect the names of all variables which have been
    // altered by a converter.
    // This serves two purposes:
    // 1. It might prevent converters later in the pipeline to process
    //    a variable again.
    // 2. To check if upcasting was successfull after each converter had
    //    a go. See below.
    $converters = array();
    $route = $defaults[RouteObjectInterface::ROUTE_OBJECT];
    foreach ($this->converters as $converter) {
      $converter
        ->process($defaults, $route, $converters);
    }

    // Check if all upcasting yielded a result.
    // If an upcast value is NULL do a 404.
    foreach ($converters as $variable) {
      if ($defaults[$variable] === NULL) {
        throw new NotFoundHttpException();
      }
    }
    return $defaults;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ParamConverterManager::$converters protected property Converters managed by the ParamConverterManager.
ParamConverterManager::addConverter public function Adds a converter to the paramconverter service.
ParamConverterManager::enhance public function Implements \Symfony\Cmf\Component\Routing\Enhancer\ŖouteEnhancerIterface. Overrides RouteEnhancerInterface::enhance