3 namespace Drupal\Core\Entity\Enhancer;
5 use Drupal\Core\Routing\EnhancerInterface;
6 use Symfony\Component\HttpFoundation\Request;
7 use Symfony\Cmf\Component\Routing\RouteObjectInterface;
8 use Symfony\Component\Routing\Route;
11 * Enhances an entity form route with the appropriate controller.
13 class EntityRouteEnhancer implements EnhancerInterface {
18 public function enhance(array $defaults, Request $request) {
19 $route = $defaults[RouteObjectInterface::ROUTE_OBJECT];
20 if (!$this->applies($route)) {
24 if (empty($defaults['_controller'])) {
25 if (!empty($defaults['_entity_form'])) {
26 $defaults = $this->enhanceEntityForm($defaults, $request);
28 elseif (!empty($defaults['_entity_list'])) {
29 $defaults = $this->enhanceEntityList($defaults, $request);
31 elseif (!empty($defaults['_entity_view'])) {
32 $defaults = $this->enhanceEntityView($defaults, $request);
39 * Returns whether the enhancer runs on the current route.
41 * @param \Symfony\Component\Routing\Route $route
46 protected function applies(Route $route) {
47 return !$route->hasDefault('_controller') &&
48 ($route->hasDefault('_entity_form')
49 || $route->hasDefault('_entity_list')
50 || $route->hasDefault('_entity_view')
55 * Update defaults for entity forms.
57 * @param array $defaults
58 * The defaults to modify.
59 * @param \Symfony\Component\HttpFoundation\Request $request
60 * The Request instance.
63 * The modified defaults.
65 protected function enhanceEntityForm(array $defaults, Request $request) {
66 $defaults['_controller'] = 'controller.entity_form:getContentResult';
73 * Update defaults for an entity list.
75 * @param array $defaults
76 * The defaults to modify.
77 * @param \Symfony\Component\HttpFoundation\Request $request
78 * The Request instance.
81 * The modified defaults.
83 protected function enhanceEntityList(array $defaults, Request $request) {
84 $defaults['_controller'] = '\Drupal\Core\Entity\Controller\EntityListController::listing';
85 $defaults['entity_type'] = $defaults['_entity_list'];
86 unset($defaults['_entity_list']);
92 * Update defaults for an entity view.
94 * @param array $defaults
95 * The defaults to modify.
96 * @param \Symfony\Component\HttpFoundation\Request $request
97 * The Request instance.
100 * The modified defaults.
102 * @throws \RuntimeException
103 * Thrown when an entity of a type cannot be found in a route.
105 protected function enhanceEntityView(array $defaults, Request $request) {
106 $defaults['_controller'] = '\Drupal\Core\Entity\Controller\EntityViewController::view';
107 if (strpos($defaults['_entity_view'], '.') !== FALSE) {
108 // The _entity_view entry is of the form entity_type.view_mode.
109 list($entity_type, $view_mode) = explode('.', $defaults['_entity_view']);
110 $defaults['view_mode'] = $view_mode;
113 // Only the entity type is nominated, the view mode will use the
115 $entity_type = $defaults['_entity_view'];
117 // Set by reference so that we get the upcast value.
118 if (!empty($defaults[$entity_type])) {
119 $defaults['_entity'] = &$defaults[$entity_type];
122 // The entity is not keyed by its entity_type. Attempt to find it
123 // using a converter.
124 $route = $defaults[RouteObjectInterface::ROUTE_OBJECT];
125 if ($route && is_object($route)) {
126 $options = $route->getOptions();
127 if (isset($options['parameters'])) {
128 foreach ($options['parameters'] as $name => $details) {
129 if (!empty($details['type'])) {
130 $type = $details['type'];
131 // Type is of the form entity:{entity_type}.
132 $parameter_entity_type = substr($type, strlen('entity:'));
133 if ($entity_type == $parameter_entity_type) {
134 // We have the matching entity type. Set the '_entity' key
135 // to point to this named placeholder. The entity in this
136 // position is the one being rendered.
137 $defaults['_entity'] = &$defaults[$name];
143 throw new \RuntimeException(sprintf('Failed to find entity of type %s in route named %s', $entity_type, $defaults[RouteObjectInterface::ROUTE_NAME]));
147 throw new \RuntimeException(sprintf('Failed to find entity of type %s in route named %s', $entity_type, $defaults[RouteObjectInterface::ROUTE_NAME]));
150 unset($defaults['_entity_view']);