3 namespace Drupal\Core\Render\MainContent;
5 use Drupal\Core\Ajax\AjaxResponse;
6 use Drupal\Core\Controller\TitleResolverInterface;
7 use Drupal\Core\Render\RendererInterface;
8 use Drupal\Core\Routing\RouteMatchInterface;
9 use Drupal\Core\Ajax\OpenOffCanvasDialogCommand;
10 use Symfony\Component\HttpFoundation\Request;
13 * Default main content renderer for off-canvas dialog requests.
17 class OffCanvasRenderer extends DialogRenderer {
22 * @var \Drupal\Core\Render\RendererInterface
27 * Constructs a new OffCanvasRenderer.
29 * @param \Drupal\Core\Controller\TitleResolverInterface $title_resolver
31 * @param \Drupal\Core\Render\RendererInterface $renderer
34 public function __construct(TitleResolverInterface $title_resolver, RendererInterface $renderer) {
35 parent::__construct($title_resolver);
36 $this->renderer = $renderer;
42 public function renderResponse(array $main_content, Request $request, RouteMatchInterface $route_match) {
43 $response = new AjaxResponse();
45 // First render the main content, because it might provide a title.
46 $content = $this->renderer->renderRoot($main_content);
47 // Attach the library necessary for using the OpenOffCanvasDialogCommand and
48 // set the attachments for this Ajax response.
49 $main_content['#attached']['library'][] = 'core/drupal.dialog.off_canvas';
50 $response->setAttachments($main_content['#attached']);
52 // If the main content doesn't provide a title, use the title resolver.
53 $title = isset($main_content['#title']) ? $main_content['#title'] : $this->titleResolver->getTitle($request, $route_match->getRouteObject());
55 // Determine the title: use the title provided by the main content if any,
56 // otherwise get it from the routing information.
57 $options = $request->request->get('dialogOptions', []);
58 $response->addCommand(new OpenOffCanvasDialogCommand($title, $content, $options));