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