function EntityResolverManager::setParametersFromEntityInformation

Same name and namespace in other branches
  1. 9 core/lib/Drupal/Core/Entity/EntityResolverManager.php \Drupal\Core\Entity\EntityResolverManager::setParametersFromEntityInformation()
  2. 8.9.x core/lib/Drupal/Core/Entity/EntityResolverManager.php \Drupal\Core\Entity\EntityResolverManager::setParametersFromEntityInformation()
  3. 10 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 174

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')) {
        [
            $entity_type,
        ] = explode('.', $entity_view, 2);
    }
    elseif ($entity_form = $route->getDefault('_entity_form')) {
        [
            $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]) && str_contains($route->getPath(), '{' . $entity_type . '}')) {
        $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']) && str_starts_with($info['type'], 'entity:')) {
                // The parameter types are in the form 'entity:$entity_type'.
                [
                    ,
                    $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);
        }
    }
}

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