287464985ba7e5f88e952389b8bc3bab13ca5818
[yaffs-website] / web / core / lib / Drupal / Core / Entity / Enhancer / EntityRouteEnhancer.php
1 <?php
2
3 namespace Drupal\Core\Entity\Enhancer;
4
5 use Drupal\Core\Routing\Enhancer\RouteEnhancerInterface;
6 use Symfony\Component\HttpFoundation\Request;
7 use Symfony\Component\Routing\Route;
8 use Symfony\Cmf\Component\Routing\RouteObjectInterface;
9
10 /**
11  * Enhances an entity form route with the appropriate controller.
12  */
13 class EntityRouteEnhancer implements RouteEnhancerInterface {
14
15   /**
16    * {@inheritdoc}
17    */
18   public function enhance(array $defaults, Request $request) {
19     if (empty($defaults['_controller'])) {
20       if (!empty($defaults['_entity_form'])) {
21         $defaults = $this->enhanceEntityForm($defaults, $request);
22       }
23       elseif (!empty($defaults['_entity_list'])) {
24         $defaults = $this->enhanceEntityList($defaults, $request);
25       }
26       elseif (!empty($defaults['_entity_view'])) {
27         $defaults = $this->enhanceEntityView($defaults, $request);
28       }
29     }
30     return $defaults;
31   }
32
33   /**
34    * {@inheritdoc}
35    */
36   public function applies(Route $route) {
37     return !$route->hasDefault('_controller') &&
38       ($route->hasDefault('_entity_form')
39         || $route->hasDefault('_entity_list')
40         || $route->hasDefault('_entity_view')
41       );
42   }
43
44   /**
45    * Update defaults for entity forms.
46    *
47    * @param array $defaults
48    *   The defaults to modify.
49    * @param \Symfony\Component\HttpFoundation\Request $request
50    *   The Request instance.
51    *
52    * @return array
53    *   The modified defaults.
54    */
55   protected function enhanceEntityForm(array $defaults, Request $request) {
56     $defaults['_controller'] = 'controller.entity_form:getContentResult';
57
58     return $defaults;
59   }
60
61
62   /**
63    * Update defaults for an entity list.
64    *
65    * @param array $defaults
66    *   The defaults to modify.
67    * @param \Symfony\Component\HttpFoundation\Request $request
68    *   The Request instance.
69    *
70    * @return array
71    *   The modified defaults.
72    */
73   protected function enhanceEntityList(array $defaults, Request $request) {
74     $defaults['_controller'] = '\Drupal\Core\Entity\Controller\EntityListController::listing';
75     $defaults['entity_type'] = $defaults['_entity_list'];
76     unset($defaults['_entity_list']);
77
78     return $defaults;
79   }
80
81   /**
82    * Update defaults for an entity view.
83    *
84    * @param array $defaults
85    *   The defaults to modify.
86    * @param \Symfony\Component\HttpFoundation\Request $request
87    *   The Request instance.
88    *
89    * @return array
90    *   The modified defaults.
91    *
92    * @throws \RuntimeException
93    *   Thrown when an entity of a type cannot be found in a route.
94    */
95   protected function enhanceEntityView(array $defaults, Request $request) {
96     $defaults['_controller'] = '\Drupal\Core\Entity\Controller\EntityViewController::view';
97     if (strpos($defaults['_entity_view'], '.') !== FALSE) {
98       // The _entity_view entry is of the form entity_type.view_mode.
99       list($entity_type, $view_mode) = explode('.', $defaults['_entity_view']);
100       $defaults['view_mode'] = $view_mode;
101     }
102     else {
103       // Only the entity type is nominated, the view mode will use the
104       // default.
105       $entity_type = $defaults['_entity_view'];
106     }
107     // Set by reference so that we get the upcast value.
108     if (!empty($defaults[$entity_type])) {
109       $defaults['_entity'] = &$defaults[$entity_type];
110     }
111     else {
112       // The entity is not keyed by its entity_type. Attempt to find it
113       // using a converter.
114       $route = $defaults[RouteObjectInterface::ROUTE_OBJECT];
115       if ($route && is_object($route)) {
116         $options = $route->getOptions();
117         if (isset($options['parameters'])) {
118           foreach ($options['parameters'] as $name => $details) {
119             if (!empty($details['type'])) {
120               $type = $details['type'];
121               // Type is of the form entity:{entity_type}.
122               $parameter_entity_type = substr($type, strlen('entity:'));
123               if ($entity_type == $parameter_entity_type) {
124                 // We have the matching entity type. Set the '_entity' key
125                 // to point to this named placeholder. The entity in this
126                 // position is the one being rendered.
127                 $defaults['_entity'] = &$defaults[$name];
128               }
129             }
130           }
131         }
132         else {
133           throw new \RuntimeException(sprintf('Failed to find entity of type %s in route named %s', $entity_type, $defaults[RouteObjectInterface::ROUTE_NAME]));
134         }
135       }
136       else {
137         throw new \RuntimeException(sprintf('Failed to find entity of type %s in route named %s', $entity_type, $defaults[RouteObjectInterface::ROUTE_NAME]));
138       }
139     }
140     unset($defaults['_entity_view']);
141
142     return $defaults;
143   }
144
145 }