3 namespace Drupal\dialog_renderer_test\Render\MainContent;
5 use Drupal\Core\Ajax\AjaxResponse;
6 use Drupal\Core\Ajax\OpenModalDialogCommand;
7 use Drupal\Core\Controller\TitleResolverInterface;
8 use Drupal\Core\Render\MainContent\ModalRenderer;
9 use Drupal\Core\Routing\RouteMatchInterface;
10 use Symfony\Component\HttpFoundation\Request;
13 * Default main content renderer for wide modal dialog requests.
15 * This test class is copied from \Drupal\Core\Render\MainContent\ModalRenderer
16 * to demonstrate selecting a different render via 'data-dialog-renderer' link
19 class WideModalRenderer extends ModalRenderer {
22 * The mode, either 'wide' or 'extra_wide'.
29 * Constructs a new WideModalRenderer.
31 * @param \Drupal\Core\Controller\TitleResolverInterface $title_resolver
34 * The mode, either 'wide' or 'extra_wide'.
36 public function __construct(TitleResolverInterface $title_resolver, $mode = 'wide') {
37 parent::__construct($title_resolver);
45 public function renderResponse(array $main_content, Request $request, RouteMatchInterface $route_match) {
46 $response = new AjaxResponse();
48 // First render the main content, because it might provide a title.
49 $content = drupal_render_root($main_content);
51 // Attach the library necessary for using the OpenModalDialogCommand and set
52 // the attachments for this Ajax response.
53 $main_content['#attached']['library'][] = 'core/drupal.dialog.ajax';
54 $response->setAttachments($main_content['#attached']);
56 // If the main content doesn't provide a title, use the title resolver.
57 $title = isset($main_content['#title']) ? $main_content['#title'] : $this->titleResolver->getTitle($request, $route_match->getRouteObject());
59 // Determine the title: use the title provided by the main content if any,
60 // otherwise get it from the routing information.
61 $options = $request->request->get('dialogOptions', []);
62 // Override width option.
63 switch ($this->mode) {
65 $options['width'] = 700;
69 $options['width'] = 1000;
73 $response->addCommand(new OpenModalDialogCommand($title, $content, $options));