class AjaxRenderer

Same name in other branches
  1. 9 core/lib/Drupal/Core/Render/MainContent/AjaxRenderer.php \Drupal\Core\Render\MainContent\AjaxRenderer
  2. 10 core/lib/Drupal/Core/Render/MainContent/AjaxRenderer.php \Drupal\Core\Render\MainContent\AjaxRenderer
  3. 11.x core/lib/Drupal/Core/Render/MainContent/AjaxRenderer.php \Drupal\Core\Render\MainContent\AjaxRenderer

Default main content renderer for Ajax requests.

Hierarchy

Expanded class hierarchy of AjaxRenderer

1 file declares its use of AjaxRenderer
AjaxRendererTest.php in core/tests/Drupal/Tests/Core/Controller/AjaxRendererTest.php
1 string reference to 'AjaxRenderer'
core.services.yml in core/core.services.yml
core/core.services.yml
1 service uses AjaxRenderer
main_content_renderer.ajax in core/core.services.yml
Drupal\Core\Render\MainContent\AjaxRenderer

File

core/lib/Drupal/Core/Render/MainContent/AjaxRenderer.php, line 17

Namespace

Drupal\Core\Render\MainContent
View source
class AjaxRenderer implements MainContentRendererInterface {
    
    /**
     * The element info manager.
     *
     * @var \Drupal\Core\Render\ElementInfoManagerInterface
     */
    protected $elementInfoManager;
    
    /**
     * The renderer.
     *
     * @var \Drupal\Core\Render\RendererInterface
     */
    protected $renderer;
    
    /**
     * Constructs a new AjaxRenderer instance.
     *
     * @param \Drupal\Core\Render\ElementInfoManagerInterface $element_info_manager
     *   The element info manager.
     * @param \Drupal\Core\Render\RendererInterface $renderer
     *   The renderer.
     */
    public function __construct(ElementInfoManagerInterface $element_info_manager, RendererInterface $renderer = NULL) {
        $this->elementInfoManager = $element_info_manager;
        if ($renderer === NULL) {
            @trigger_error('The renderer service must be passed to ' . __METHOD__ . ' and will be required before Drupal 9.0.0. See https://www.drupal.org/node/3009400', E_USER_DEPRECATED);
            $renderer = \Drupal::service('renderer');
        }
        $this->renderer = $renderer;
    }
    
    /**
     * {@inheritdoc}
     */
    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->renderer
            ->renderRoot($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 = [
            '#type' => 'status_messages',
        ];
        $output = $this->renderer
            ->renderRoot($status_messages);
        if (!empty($output)) {
            $response->addCommand(new PrependCommand(NULL, $output));
        }
        return $response;
    }
    
    /**
     * Wraps \Drupal\Core\Render\RendererInterface::renderRoot().
     *
     * @deprecated in drupal:8.7.0 and is removed from drupal:9.0.0. Use
     *   $this->renderer->renderRoot() instead.
     *
     * @see https://www.drupal.org/node/2912696
     */
    protected function drupalRenderRoot(&$elements) {
        @trigger_error('\\Drupal\\Core\\Render\\MainContent\\AjaxRenderer::drupalRenderRoot() is deprecated in Drupal 8.7.x and will be removed before Drupal 9.0.0. Use $this->renderer->renderRoot() instead. See https://www.drupal.org/node/2912696', E_USER_DEPRECATED);
        return $this->renderer
            ->renderRoot($elements);
    }

}

Members

Title Sort descending Deprecated Modifiers Object type Summary Overriden Title
AjaxRenderer::$elementInfoManager protected property The element info manager.
AjaxRenderer::$renderer protected property The renderer.
AjaxRenderer::drupalRenderRoot Deprecated protected function Wraps \Drupal\Core\Render\RendererInterface::renderRoot().
AjaxRenderer::renderResponse public function Renders the main content render array into a response. Overrides MainContentRendererInterface::renderResponse
AjaxRenderer::__construct public function Constructs a new AjaxRenderer instance.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.