/** * Catches a form AJAX exception and build a response from it. * * @param \Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent $event * The event to process. */ public function onException(GetResponseForExceptionEvent $event) { $exception = $event->getException(); $request = $event->getRequest(); // Render a nice error message in case we have a file upload which exceeds // the configured upload limit. if ($exception instanceof BrokenPostRequestException && $request->query->has(FormBuilderInterface::AJAX_FORM_REQUEST)) { $this->drupalSetMessage($this->t('An unrecoverable error occurred. The uploaded file likely exceeded the maximum file size (@size) that this server supports.', ['@size' => $this->formatSize($exception->getSize())]), 'error'); $response = new AjaxResponse(); $status_messages = ['#type' => 'status_messages']; $response->addCommand(new ReplaceCommand(NULL, $status_messages)); $response->headers->set('X-Status-Code', 200); $event->setResponse($response); return; } // Extract the form AJAX exception (it may have been passed to another // exception before reaching here). if ($exception = $this->getFormAjaxException($exception)) { $request = $event->getRequest(); $form = $exception->getForm(); $form_state = $exception->getFormState(); // Set the build ID from the request as the old build ID on the form. $form['#build_id_old'] = $request->get('form_build_id'); try { $response = $this->formAjaxResponseBuilder->buildResponse($request, $form, $form_state, []); // Since this response is being set in place of an exception, explicitly // mark this as a 200 status. $response->headers->set('X-Status-Code', 200); $event->setResponse($response); } catch (\Exception $e) { // Otherwise, replace the existing exception with the new one. $event->setException($e); } } }
/** * @covers ::getFormAjaxException */ public function testOnExceptionNestedWrongException() { $nested_exception = new \Exception(); $exception = new \Exception('', 0, $nested_exception); $request = new Request(); $this->formAjaxResponseBuilder->expects($this->never())->method('buildResponse'); $this->assertResponseFromException($request, $exception, NULL); }
/** * Processes an Ajax form submission. * * @param \Symfony\Component\HttpFoundation\Request $request * The current request object. * * @return mixed * Whatever is returned by the triggering element's #ajax['callback'] * function. One of: * - A render array containing the new or updated content to return to the * browser. This is commonly an element within the rebuilt form. * - A \Drupal\Core\Ajax\AjaxResponse object containing commands for the * browser to process. * * @throws \Symfony\Component\HttpKernel\Exception\HttpExceptionInterface */ public function content(Request $request) { $ajax_form = $this->getForm($request); $form = $ajax_form->getForm(); $form_state = $ajax_form->getFormState(); $commands = $ajax_form->getCommands(); $this->formBuilder->processForm($form['#form_id'], $form, $form_state); return $this->formAjaxResponseBuilder->buildResponse($request, $form, $form_state, $commands); }