EntityResolverManager::setParametersFromEntityInformation

protected EntityResolverManager::setParametersFromEntityInformation(Route $route)

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.

File

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

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') ? : array();

    // 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] = array();
    }
    $parameter_definitions[$entity_type] += array(
      'type' => 'entity:' . $entity_type,
    );
    if (!empty($parameter_definitions)) {
      $route->setOption('parameters', $parameter_definitions);
    }
  }
}
doc_Drupal
2016-10-29 09:07:27
Comments
Leave a Comment

Please login to continue.