class MainContentViewSubscriber in Zircon Profile 8.0
Same name and namespace in other branches
- 8 core/lib/Drupal/Core/EventSubscriber/MainContentViewSubscriber.php \Drupal\Core\EventSubscriber\MainContentViewSubscriber
View subscriber rendering main content render arrays into responses.
Additional target rendering formats can be defined by adding another service that implements \Drupal\Core\Render\MainContent\MainContentRendererInterface and tagging it as a
render . main_content_renderer;
, then \Drupal\Core\Render\MainContent\MainContentRenderersPass will detect it and use it when appropriate.
Hierarchy
- class \Drupal\Core\EventSubscriber\MainContentViewSubscriber implements EventSubscriberInterface
Expanded class hierarchy of MainContentViewSubscriber
See also
\Drupal\Core\Render\MainContent\MainContentRendererInterface
\Drupal\Core\Render\MainContentControllerPass
16 files declare their use of MainContentViewSubscriber
- AjaxFormCacheTest.php in core/
modules/ system/ src/ Tests/ Ajax/ AjaxFormCacheTest.php - Contains \Drupal\system\Tests\Ajax\AjaxFormCacheTest.
- BlockLibraryController.php in core/
modules/ block/ src/ Controller/ BlockLibraryController.php - Contains \Drupal\block\Controller\BlockLibraryController.
- DialogTest.php in core/
modules/ system/ src/ Tests/ Ajax/ DialogTest.php - Contains \Drupal\system\Tests\Ajax\DialogTest.
- DynamicPageCacheIntegrationTest.php in core/
modules/ dynamic_page_cache/ src/ Tests/ DynamicPageCacheIntegrationTest.php - Contains \Drupal\dynamic_page_cache\Tests\DynamicPageCacheIntegrationTest.
- EntityCacheTagsTestBase.php in core/
modules/ system/ src/ Tests/ Entity/ EntityCacheTagsTestBase.php - Contains \Drupal\system\Tests\Entity\EntityCacheTagsTestBase.
1 string reference to 'MainContentViewSubscriber'
- core.services.yml in core/
core.services.yml - core/core.services.yml
1 service uses MainContentViewSubscriber
File
- core/
lib/ Drupal/ Core/ EventSubscriber/ MainContentViewSubscriber.php, line 30 - Contains \Drupal\Core\EventSubscriber\MainContentViewSubscriber.
Namespace
Drupal\Core\EventSubscriberView source
class MainContentViewSubscriber implements EventSubscriberInterface {
/**
* The class resolver service.
*
* @var \Drupal\Core\Controller\ControllerResolverInterface
*/
protected $classResolver;
/**
* The current route match.
*
* @var \Drupal\Core\Routing\RouteMatchInterface
*/
protected $routeMatch;
/**
* The available main content renderer services, keyed per format.
*
* @var array
*/
protected $mainContentRenderers;
/**
* URL query attribute to indicate the wrapper used to render a request.
*
* The wrapper format determines how the HTML is wrapped, for example in a
* modal dialog.
*/
const WRAPPER_FORMAT = '_wrapper_format';
/**
* Constructs a new MainContentViewSubscriber object.
*
* @param \Drupal\Core\DependencyInjection\ClassResolverInterface $class_resolver
* The class resolver service.
* @param \Drupal\Core\Routing\RouteMatchInterface $route_match
* The current route match.
* @param array $main_content_renderers
* The available main content renderer service IDs, keyed by format.
*/
public function __construct(ClassResolverInterface $class_resolver, RouteMatchInterface $route_match, array $main_content_renderers) {
$this->classResolver = $class_resolver;
$this->routeMatch = $route_match;
$this->mainContentRenderers = $main_content_renderers;
}
/**
* Sets a response given a (main content) render array.
*
* @param \Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent $event
* The event to process.
*/
public function onViewRenderArray(GetResponseForControllerResultEvent $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);
}
}
/**
* {@inheritdoc}
*/
static function getSubscribedEvents() {
$events[KernelEvents::VIEW][] = [
'onViewRenderArray',
];
return $events;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
MainContentViewSubscriber:: |
protected | property | The class resolver service. | |
MainContentViewSubscriber:: |
protected | property | The available main content renderer services, keyed per format. | |
MainContentViewSubscriber:: |
protected | property | The current route match. | |
MainContentViewSubscriber:: |
static | function |
Returns an array of event names this subscriber wants to listen to. Overrides EventSubscriberInterface:: |
|
MainContentViewSubscriber:: |
public | function | Sets a response given a (main content) render array. | |
MainContentViewSubscriber:: |
constant | URL query attribute to indicate the wrapper used to render a request. | ||
MainContentViewSubscriber:: |
public | function | Constructs a new MainContentViewSubscriber object. |