public FormAjaxSubscriber::onException(GetResponseForExceptionEvent $event)
Catches a form AJAX exception and build a response from it.
Parameters
\Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent $event: The event to process.
File
- core/lib/Drupal/Core/Form/EventSubscriber/FormAjaxSubscriber.php, line 71
Class
- FormAjaxSubscriber
- Wraps AJAX form submissions that are triggered via an exception.
Namespace
Drupal\Core\Form\EventSubscriber
Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | 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 ->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 ); } } } |
Please login to continue.