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';
72 * Update defaults for an entity list.
74 * @param array $defaults
75 * The defaults to modify.
76 * @param \Symfony\Component\HttpFoundation\Request $request
77 * The Request instance.
80 * The modified defaults.
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']);
91 * Update defaults for an entity view.
93 * @param array $defaults
94 * The defaults to modify.
95 * @param \Symfony\Component\HttpFoundation\Request $request
96 * The Request instance.
99 * The modified defaults.
101 * @throws \RuntimeException
102 * Thrown when an entity of a type cannot be found in a route.
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;
112 // Only the entity type is nominated, the view mode will use the
114 $entity_type = $defaults['_entity_view'];
116 // Set by reference so that we get the upcast value.
117 if (!empty($defaults[$entity_type])) {
118 $defaults['_entity'] = &$defaults[$entity_type];
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];
142 throw new \RuntimeException(sprintf('Failed to find entity of type %s in route named %s', $entity_type, $defaults[RouteObjectInterface::ROUTE_NAME]));
146 throw new \RuntimeException(sprintf('Failed to find entity of type %s in route named %s', $entity_type, $defaults[RouteObjectInterface::ROUTE_NAME]));
149 unset($defaults['_entity_view']);