EntityRouteEnhancer.php in Drupal 9
File
core/lib/Drupal/Core/Entity/Enhancer/EntityRouteEnhancer.php
View source
<?php
namespace Drupal\Core\Entity\Enhancer;
use Drupal\Core\Routing\EnhancerInterface;
use Symfony\Component\HttpFoundation\Request;
use Drupal\Core\Routing\RouteObjectInterface;
use Symfony\Component\Routing\Route;
class EntityRouteEnhancer implements EnhancerInterface {
public function enhance(array $defaults, Request $request) {
$route = $defaults[RouteObjectInterface::ROUTE_OBJECT];
if (!$this
->applies($route)) {
return $defaults;
}
if (empty($defaults['_controller'])) {
if (!empty($defaults['_entity_form'])) {
$defaults = $this
->enhanceEntityForm($defaults, $request);
}
elseif (!empty($defaults['_entity_list'])) {
$defaults = $this
->enhanceEntityList($defaults, $request);
}
elseif (!empty($defaults['_entity_view'])) {
$defaults = $this
->enhanceEntityView($defaults, $request);
}
}
return $defaults;
}
protected function applies(Route $route) {
return !$route
->hasDefault('_controller') && ($route
->hasDefault('_entity_form') || $route
->hasDefault('_entity_list') || $route
->hasDefault('_entity_view'));
}
protected function enhanceEntityForm(array $defaults, Request $request) {
$defaults['_controller'] = 'controller.entity_form:getContentResult';
return $defaults;
}
protected function enhanceEntityList(array $defaults, Request $request) {
$defaults['_controller'] = '\\Drupal\\Core\\Entity\\Controller\\EntityListController::listing';
$defaults['entity_type'] = $defaults['_entity_list'];
unset($defaults['_entity_list']);
return $defaults;
}
protected function enhanceEntityView(array $defaults, Request $request) {
$defaults['_controller'] = '\\Drupal\\Core\\Entity\\Controller\\EntityViewController::view';
if (strpos($defaults['_entity_view'], '.') !== FALSE) {
list($entity_type, $view_mode) = explode('.', $defaults['_entity_view']);
$defaults['view_mode'] = $view_mode;
}
else {
$entity_type = $defaults['_entity_view'];
}
if (!empty($defaults[$entity_type])) {
$defaults['_entity'] =& $defaults[$entity_type];
}
else {
$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'];
$parameter_entity_type = substr($type, strlen('entity:'));
if ($entity_type == $parameter_entity_type) {
$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;
}
}