public function EntityConverter::process

Tries to upcast every variable to an entity type.

If there is a type denoted in the route options it will try to upcast to it, if there is no definition in the options it will try to upcast to an entity type of that name. If the chosen enity type does not exists it will leave the variable untouched. If the entity type exist, but there is no entity with the given id it will convert the variable to NULL.

Example:

pattern: '/a/{user}/some/{foo}/and/{bar}/' options: converters: foo: 'node'

The value for {user} will be converted to a user entity and the value for {foo} to a node entity, but it will not touch the value for {bar}.

It will not process variables which are marked as converted. It will mark any variable it processes as converted.

Parameters

array &$variables: Array of values to convert to their corresponding objects, if applicable.

\Symfony\Component\Routing\Route $route: The route object.

array &$converted: Array collecting the names of all variables which have been altered by a converter.

Overrides ParamConverterInterface::process

File

drupal/core/lib/Drupal/Core/ParamConverter/EntityConverter.php, line 68
Contains Drupal\Core\ParamConverter\EntityConverter.

Class

EntityConverter
This class allows the upcasting of entity ids to the respective entity object.

Namespace

Drupal\Core\ParamConverter

Code

public function process(array &$variables, Route $route, array &$converted) {
  $variable_names = $route
    ->compile()
    ->getVariables();
  $options = $route
    ->getOptions();
  $configuredTypes = isset($options['converters']) ? $options['converters'] : array();
  $entityTypes = array_keys($this->entityManager
    ->getDefinitions());
  foreach ($variable_names as $name) {

    // Do not process this variable if it's already marked as converted.
    if (in_array($name, $converted)) {
      continue;
    }

    // Obtain entity type to convert to from the route configuration or just
    // use the variable name as default.
    if (array_key_exists($name, $configuredTypes)) {
      $type = $configuredTypes[$name];
    }
    else {
      $type = $name;
    }
    if (in_array($type, $entityTypes)) {
      $value = $variables[$name];
      $storageController = $this->entityManager
        ->getStorageController($type);
      $entities = $storageController
        ->load(array(
        $value,
      ));

      // Make sure $entities is null, if upcasting fails.
      $entity = $entities ? reset($entities) : NULL;
      $variables[$name] = $entity;

      // Mark this variable as converted.
      $converted[] = $name;
    }
  }
}