Updated to Drupal 8.5. Core Media not yet in use.
[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   /**
73    * Update defaults for an entity list.
74    *
75    * @param array $defaults
76    *   The defaults to modify.
77    * @param \Symfony\Component\HttpFoundation\Request $request
78    *   The Request instance.
79    *
80    * @return array
81    *   The modified defaults.
82    */
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']);
87
88     return $defaults;
89   }
90
91   /**
92    * Update defaults for an entity view.
93    *
94    * @param array $defaults
95    *   The defaults to modify.
96    * @param \Symfony\Component\HttpFoundation\Request $request
97    *   The Request instance.
98    *
99    * @return array
100    *   The modified defaults.
101    *
102    * @throws \RuntimeException
103    *   Thrown when an entity of a type cannot be found in a route.
104    */
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;
111     }
112     else {
113       // Only the entity type is nominated, the view mode will use the
114       // default.
115       $entity_type = $defaults['_entity_view'];
116     }
117     // Set by reference so that we get the upcast value.
118     if (!empty($defaults[$entity_type])) {
119       $defaults['_entity'] = &$defaults[$entity_type];
120     }
121     else {
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];
138               }
139             }
140           }
141         }
142         else {
143           throw new \RuntimeException(sprintf('Failed to find entity of type %s in route named %s', $entity_type, $defaults[RouteObjectInterface::ROUTE_NAME]));
144         }
145       }
146       else {
147         throw new \RuntimeException(sprintf('Failed to find entity of type %s in route named %s', $entity_type, $defaults[RouteObjectInterface::ROUTE_NAME]));
148       }
149     }
150     unset($defaults['_entity_view']);
151
152     return $defaults;
153   }
154
155 }