You are here

protected function EntityResolverManager::setParametersFromEntityInformation in Drupal 9

Same name and namespace in other branches
  1. 8 core/lib/Drupal/Core/Entity/EntityResolverManager.php \Drupal\Core\Entity\EntityResolverManager::setParametersFromEntityInformation()

Sets the upcasting information using the _entity_* route defaults.

Supports the '_entity_view' and '_entity_form' route defaults.

Parameters

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

1 call to EntityResolverManager::setParametersFromEntityInformation()
EntityResolverManager::setRouteOptions in core/lib/Drupal/Core/Entity/EntityResolverManager.php
Set the upcasting route objects.

File

core/lib/Drupal/Core/Entity/EntityResolverManager.php, line 167

Class

EntityResolverManager
Sets the entity route parameter converter options automatically.

Namespace

Drupal\Core\Entity

Code

protected function setParametersFromEntityInformation(Route $route) {
  if ($entity_view = $route
    ->getDefault('_entity_view')) {
    list($entity_type) = explode('.', $entity_view, 2);
  }
  elseif ($entity_form = $route
    ->getDefault('_entity_form')) {
    list($entity_type) = explode('.', $entity_form, 2);
  }

  // Do not add parameter information if the route does not declare a
  // parameter in the first place. This is the case for add forms, for
  // example.
  if (isset($entity_type) && isset($this
    ->getEntityTypes()[$entity_type]) && strpos($route
    ->getPath(), '{' . $entity_type . '}') !== FALSE) {
    $parameter_definitions = $route
      ->getOption('parameters') ?: [];

    // First try to figure out whether there is already a parameter upcasting
    // the same entity type already.
    foreach ($parameter_definitions as $info) {
      if (isset($info['type']) && strpos($info['type'], 'entity:') === 0) {

        // The parameter types are in the form 'entity:$entity_type'.
        list(, $parameter_entity_type) = explode(':', $info['type'], 2);
        if ($parameter_entity_type == $entity_type) {
          return;
        }
      }
    }
    if (!isset($parameter_definitions[$entity_type])) {
      $parameter_definitions[$entity_type] = [];
    }
    $parameter_definitions[$entity_type] += [
      'type' => 'entity:' . $entity_type,
    ];
    if (!empty($parameter_definitions)) {
      $route
        ->setOption('parameters', $parameter_definitions);
    }
  }
}