EntityRouteEnhancer::enhanceEntityView

protected EntityRouteEnhancer::enhanceEntityView(array $defaults, Request $request)

Update defaults for an entity view.

Parameters

array $defaults: The defaults to modify.

\Symfony\Component\HttpFoundation\Request $request: The Request instance.

Return value

array The modified defaults.

Throws

\RuntimeException Thrown when an entity of a type cannot be found in a route.

File

core/lib/Drupal/Core/Entity/Enhancer/EntityRouteEnhancer.php, line 95

Class

EntityRouteEnhancer
Enhances an entity form route with the appropriate controller.

Namespace

Drupal\Core\Entity\Enhancer

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
protected function enhanceEntityView(array $defaults, Request $request) {
  $defaults['_controller'] = '\Drupal\Core\Entity\Controller\EntityViewController::view';
  if (strpos($defaults['_entity_view'], '.') !== FALSE) {
    // The _entity_view entry is of the form entity_type.view_mode.
    list($entity_type, $view_mode) = explode('.', $defaults['_entity_view']);
    $defaults['view_mode'] = $view_mode;
  }
  else {
    // Only the entity type is nominated, the view mode will use the
    // default.
    $entity_type = $defaults['_entity_view'];
  }
  // Set by reference so that we get the upcast value.
  if (!empty($defaults[$entity_type])) {
    $defaults['_entity'] = &$defaults[$entity_type];
  }
  else {
    // The entity is not keyed by its entity_type. Attempt to find it
    // using a converter.
    $route = $defaults[RouteObjectInterface::ROUTE_OBJECT];
    if ($route && is_object($route)) {
      $options = $route->getOptions();
      if (isset($options['parameters'])) {
        foreach ($options['parameters'] as $name => $details) {
          if (!empty($details['type'])) {
            $type = $details['type'];
            // Type is of the form entity:{entity_type}.
            $parameter_entity_type = substr($type, strlen('entity:'));
            if ($entity_type == $parameter_entity_type) {
              // We have the matching entity type. Set the '_entity' key
              // to point to this named placeholder. The entity in this
              // position is the one being rendered.
              $defaults['_entity'] = &$defaults[$name];
            }
          }
        }
      }
      else {
        throw new \RuntimeException(sprintf('Failed to find entity of type %s in route named %s', $entity_type, $defaults[RouteObjectInterface::ROUTE_NAME]));
      }
    }
    else {
      throw new \RuntimeException(sprintf('Failed to find entity of type %s in route named %s', $entity_type, $defaults[RouteObjectInterface::ROUTE_NAME]));
    }
  }
  unset($defaults['_entity_view']);
 
  return $defaults;
}
doc_Drupal
2025-01-10 15:47:30
Comments
Leave a Comment

Please login to continue.