diff --git a/core/core.services.yml b/core/core.services.yml index 9dca742..83e6b94 100644 --- a/core/core.services.yml +++ b/core/core.services.yml @@ -286,7 +286,7 @@ services: tags: - { name: route_enhancer, priority: 15 } route_enhancer.form: - class: Drupal\Core\Routing\Enhancer\FormEnhancer + class: Drupal\Core\Routing\Enhancer\HtmlFormEnhancer arguments: ['@content_negotiation'] tags: - { name: route_enhancer, priority: 10 } diff --git a/core/lib/Drupal/Core/AjaxController.php b/core/lib/Drupal/Core/AjaxController.php index 31259fb..f3eed0a 100644 --- a/core/lib/Drupal/Core/AjaxController.php +++ b/core/lib/Drupal/Core/AjaxController.php @@ -50,6 +50,7 @@ public function content(Request $request, $_content) { // Remove the accept header so the subrequest does not end up back in this // controller. $request->headers->remove('accept'); + $request->headers->remove('x-requested-with'); $response = $this->container->get('http_kernel')->forward($controller, $attributes->all(), $request->query->all()); // For successful (HTTP status 200) responses. diff --git a/core/lib/Drupal/Core/AjaxFormController.php b/core/lib/Drupal/Core/AjaxFormController.php new file mode 100644 index 0000000..825c46b --- /dev/null +++ b/core/lib/Drupal/Core/AjaxFormController.php @@ -0,0 +1,23 @@ +getFormBuild($request, $_form); + return new Respnse($form); + } + +} diff --git a/core/lib/Drupal/Core/HtmlFormController.php b/core/lib/Drupal/Core/HtmlFormController.php index e19d8ea..f7890a8 100644 --- a/core/lib/Drupal/Core/HtmlFormController.php +++ b/core/lib/Drupal/Core/HtmlFormController.php @@ -46,23 +46,7 @@ public function setContainer(ContainerInterface $container = NULL) { * A response object. */ public function content(Request $request, $_form) { - $form_object = $this->getFormObject($request, $_form); - - // Using reflection, find all of the parameters needed by the form in the - // request attributes, skipping $form and $form_state. - $attributes = $request->attributes->all(); - $reflection = new \ReflectionMethod($form_object, 'buildForm'); - $params = $reflection->getParameters(); - $args = array(); - foreach (array_splice($params, 2) as $param) { - if (array_key_exists($param->name, $attributes)) { - $args[] = $attributes[$param->name]; - } - } - $form_state['build_info']['args'] = $args; - - $form_id = _drupal_form_id($form_object, $form_state); - $form = drupal_build_form($form_id, $form_state); + $form = $this->getFormBuild($request, $_form); return new Response(drupal_render_page($form)); } @@ -91,4 +75,36 @@ protected function getFormObject(Request $request, $form_arg) { return $this->container->get($form_arg); } + /** + * Build the form array for a given form. + * + * @param \Symfony\Component\HttpFoundation\Request $request + * The request object. + * @param callable $_form + * The name of the form class for this request. + * + * @return array|mixed + * The built as render array. + */ + protected function getFormBuild(Request $request, $_form) { + $form_object = $this->getFormObject($request, $_form); + + // Using reflection, find all of the parameters needed by the form in the + // request attributes, skipping $form and $form_state. + $attributes = $request->attributes->all(); + $reflection = new \ReflectionMethod($form_object, 'buildForm'); + $params = $reflection->getParameters(); + $args = array(); + foreach (array_splice($params, 2) as $param) { + if (array_key_exists($param->name, $attributes)) { + $args[] = $attributes[$param->name]; + } + } + $form_state['build_info']['args'] = $args; + + $form_id = _drupal_form_id($form_object, $form_state); + $form = drupal_build_form($form_id, $form_state); + return $form; + } + } diff --git a/core/lib/Drupal/Core/HttpKernel.php b/core/lib/Drupal/Core/HttpKernel.php index ca0c002..ce3272f 100644 --- a/core/lib/Drupal/Core/HttpKernel.php +++ b/core/lib/Drupal/Core/HttpKernel.php @@ -72,7 +72,12 @@ public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQ */ public function forward($controller, array $attributes = array(), array $query = array()) { - $attributes['_controller'] = $controller; + if (isset($controller)) { + $attributes['_controller'] = $controller; + } + else { + unset($attributes['_controller']); + } $subRequest = $this->container->get('request')->duplicate($query, null, $attributes); return $this->handle($subRequest, HttpKernelInterface::SUB_REQUEST); diff --git a/core/lib/Drupal/Core/Routing/Enhancer/AjaxFormEnhancer.php b/core/lib/Drupal/Core/Routing/Enhancer/AjaxFormEnhancer.php new file mode 100644 index 0000000..e03f944 --- /dev/null +++ b/core/lib/Drupal/Core/Routing/Enhancer/AjaxFormEnhancer.php @@ -0,0 +1,31 @@ +negotiation = $negotiation; - } - - /** - * {@inhertdoc} - */ - public function enhance(array $defaults, Request $request) { - if (empty($defaults['_controller']) && !empty($defaults['_form']) && $this->negotiation->getContentType($request) === 'html') { - $defaults['_controller'] = '\Drupal\Core\HtmlFormController::content'; - } - return $defaults; - } - -} diff --git a/core/lib/Drupal/Core/Routing/Enhancer/HtmlFormEnhancer.php b/core/lib/Drupal/Core/Routing/Enhancer/HtmlFormEnhancer.php new file mode 100644 index 0000000..3ecfe77 --- /dev/null +++ b/core/lib/Drupal/Core/Routing/Enhancer/HtmlFormEnhancer.php @@ -0,0 +1,60 @@ +negotiation = $negotiation; + } + + /** + * {@inhertdoc} + */ + public function enhance(array $defaults, Request $request) { + if (empty($defaults['_controller']) && !empty($defaults['_form']) && $this->negotiation->getContentType($request) === $this->targetContentType) { + $defaults['_controller'] = $this->controller; + } + return $defaults; + } + +}