public AjaxRenderer::renderResponse(array $main_content, Request $request, RouteMatchInterface $route_match)
Renders the main content render array into a response.
Parameters
array $main_content: The render array representing the main content.
\Symfony\Component\HttpFoundation\Request $request: The request object, for context.
\Drupal\Core\Routing\RouteMatchInterface $route_match: The route match, for context.
Return value
\Symfony\Component\HttpFoundation\Response The Response in the format that this implementation supports.
Overrides MainContentRendererInterface::renderResponse
File
- core/lib/Drupal/Core/Render/MainContent/AjaxRenderer.php, line 45
Class
- AjaxRenderer
- Default main content renderer for Ajax requests.
Namespace
Drupal\Core\Render\MainContent
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 | public function renderResponse( array $main_content , Request $request , RouteMatchInterface $route_match ) { $response = new AjaxResponse(); if (isset( $main_content [ '#type' ]) && ( $main_content [ '#type' ] == 'ajax' )) { // Complex Ajax callbacks can return a result that contains an error // message or a specific set of commands to send to the browser. $main_content += $this ->elementInfoManager->getInfo( 'ajax' ); $error = $main_content [ '#error' ]; if (! empty ( $error )) { // Fall back to some default message otherwise use the specific one. if (! is_string ( $error )) { $error = 'An error occurred while handling the request: The server received invalid input.' ; } $response ->addCommand( new AlertCommand( $error )); } } $html = $this ->drupalRenderRoot( $main_content ); $response ->setAttachments( $main_content [ '#attached' ]); // The selector for the insert command is NULL as the new content will // replace the element making the Ajax call. The default 'replaceWith' // behavior can be changed with #ajax['method']. $response ->addCommand( new InsertCommand(NULL, $html )); $status_messages = array ( '#type' => 'status_messages' ); $output = $this ->drupalRenderRoot( $status_messages ); if (! empty ( $output )) { $response ->addCommand( new PrependCommand(NULL, $output )); } return $response ; } |
Please login to continue.