3 namespace Drupal\Core\Entity\Enhancer;
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;
11 * Enhances an entity form route with the appropriate controller.
13 class EntityRouteEnhancer implements RouteEnhancerInterface {
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);
23 elseif (!empty($defaults['_entity_list'])) {
24 $defaults = $this->enhanceEntityList($defaults, $request);
26 elseif (!empty($defaults['_entity_view'])) {
27 $defaults = $this->enhanceEntityView($defaults, $request);
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')
45 * Update defaults for entity forms.
47 * @param array $defaults
48 * The defaults to modify.
49 * @param \Symfony\Component\HttpFoundation\Request $request
50 * The Request instance.
53 * The modified defaults.
55 protected function enhanceEntityForm(array $defaults, Request $request) {
56 $defaults['_controller'] = 'controller.entity_form:getContentResult';
63 * Update defaults for an entity list.
65 * @param array $defaults
66 * The defaults to modify.
67 * @param \Symfony\Component\HttpFoundation\Request $request
68 * The Request instance.
71 * The modified defaults.
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']);
82 * Update defaults for an entity view.
84 * @param array $defaults
85 * The defaults to modify.
86 * @param \Symfony\Component\HttpFoundation\Request $request
87 * The Request instance.
90 * The modified defaults.
92 * @throws \RuntimeException
93 * Thrown when an entity of a type cannot be found in a route.
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;
103 // Only the entity type is nominated, the view mode will use the
105 $entity_type = $defaults['_entity_view'];
107 // Set by reference so that we get the upcast value.
108 if (!empty($defaults[$entity_type])) {
109 $defaults['_entity'] = &$defaults[$entity_type];
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];
133 throw new \RuntimeException(sprintf('Failed to find entity of type %s in route named %s', $entity_type, $defaults[RouteObjectInterface::ROUTE_NAME]));
137 throw new \RuntimeException(sprintf('Failed to find entity of type %s in route named %s', $entity_type, $defaults[RouteObjectInterface::ROUTE_NAME]));
140 unset($defaults['_entity_view']);