Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / web / core / lib / Drupal / Core / Render / MainContent / OffCanvasRenderer.php
1 <?php
2
3 namespace Drupal\Core\Render\MainContent;
4
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;
11
12 /**
13  * Default main content renderer for off-canvas dialog requests.
14  *
15  * @internal
16  */
17 class OffCanvasRenderer extends DialogRenderer {
18
19   /**
20    * The renderer.
21    *
22    * @var \Drupal\Core\Render\RendererInterface
23    */
24   protected $renderer;
25
26   /**
27    * Constructs a new OffCanvasRenderer.
28    *
29    * @param \Drupal\Core\Controller\TitleResolverInterface $title_resolver
30    *   The title resolver.
31    * @param \Drupal\Core\Render\RendererInterface $renderer
32    *   The renderer.
33    */
34   public function __construct(TitleResolverInterface $title_resolver, RendererInterface $renderer) {
35     parent::__construct($title_resolver);
36     $this->renderer = $renderer;
37   }
38
39   /**
40    * {@inheritdoc}
41    */
42   public function renderResponse(array $main_content, Request $request, RouteMatchInterface $route_match) {
43     $response = new AjaxResponse();
44
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']);
51
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());
54
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));
59     return $response;
60   }
61
62 }