Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. MainContentViewSubscriber.php

function MainContentViewSubscriber::onViewRenderArray

Sets a response given a (main content) render array.

Parameters

\Symfony\Component\HttpKernel\Event\ViewEvent $event: The event to process.

File

core/lib/Drupal/Core/EventSubscriber/MainContentViewSubscriber.php, line 78

Class

MainContentViewSubscriber
View subscriber rendering main content render arrays into responses.

Namespace

Drupal\Core\EventSubscriber

Code

public function onViewRenderArray(ViewEvent $event) {
    $request = $event->getRequest();
    $result = $event->getControllerResult();
    // Render the controller result into a response if it's a render array.
    if (is_array($result) && ($request->query
        ->has(static::WRAPPER_FORMAT) || $request->getRequestFormat() == 'html')) {
        $wrapper = $request->query
            ->get(static::WRAPPER_FORMAT, 'html');
        // Fall back to HTML if the requested wrapper envelope is not available.
        $wrapper = isset($this->mainContentRenderers[$wrapper]) ? $wrapper : 'html';
        $renderer = $this->classResolver
            ->getInstanceFromDefinition($this->mainContentRenderers[$wrapper]);
        $response = $renderer->renderResponse($result, $request, $this->routeMatch);
        // The main content render array is rendered into a different Response
        // object, depending on the specified wrapper format.
        if ($response instanceof CacheableResponseInterface) {
            $main_content_view_subscriber_cacheability = (new CacheableMetadata())->setCacheContexts([
                'url.query_args:' . static::WRAPPER_FORMAT,
            ]);
            $response->addCacheableDependency($main_content_view_subscriber_cacheability);
        }
        $event->setResponse($response);
    }
}

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal