You are here

class WideModalRenderer in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/system/tests/modules/dialog_renderer_test/src/Render/MainContent/WideModalRenderer.php \Drupal\dialog_renderer_test\Render\MainContent\WideModalRenderer

Default main content renderer for wide modal dialog requests.

This test class is copied from \Drupal\Core\Render\MainContent\ModalRenderer to demonstrate selecting a different render via 'data-dialog-renderer' link attribute.

Hierarchy

Expanded class hierarchy of WideModalRenderer

1 string reference to 'WideModalRenderer'
dialog_renderer_test.services.yml in core/modules/system/tests/modules/dialog_renderer_test/dialog_renderer_test.services.yml
core/modules/system/tests/modules/dialog_renderer_test/dialog_renderer_test.services.yml
2 services use WideModalRenderer
main_content_renderer.extra_wide_modal in core/modules/system/tests/modules/dialog_renderer_test/dialog_renderer_test.services.yml
Drupal\dialog_renderer_test\Render\MainContent\WideModalRenderer
main_content_renderer.wide_modal in core/modules/system/tests/modules/dialog_renderer_test/dialog_renderer_test.services.yml
Drupal\dialog_renderer_test\Render\MainContent\WideModalRenderer

File

core/modules/system/tests/modules/dialog_renderer_test/src/Render/MainContent/WideModalRenderer.php, line 20

Namespace

Drupal\dialog_renderer_test\Render\MainContent
View source
class WideModalRenderer extends ModalRenderer {

  /**
   * The mode, either 'wide' or 'extra_wide'.
   *
   * @var string
   */
  protected $mode;

  /**
   * Constructs a new WideModalRenderer.
   *
   * @param \Drupal\Core\Controller\TitleResolverInterface $title_resolver
   *   The title resolver.
   * @param \Drupal\Core\Render\RendererInterface $renderer
   *   The renderer.
   * @param string $mode
   *   The mode, either 'wide' or 'extra_wide'.
   */
  public function __construct(TitleResolverInterface $title_resolver, RendererInterface $renderer, $mode = 'wide') {
    parent::__construct($title_resolver, $renderer);
    $this->mode = $mode;
  }

  /**
   * {@inheritdoc}
   */
  public function renderResponse(array $main_content, Request $request, RouteMatchInterface $route_match) {
    $response = new AjaxResponse();

    // First render the main content, because it might provide a title.
    $content = $this->renderer
      ->renderRoot($main_content);

    // Attach the library necessary for using the OpenModalDialogCommand and set
    // the attachments for this Ajax response.
    $main_content['#attached']['library'][] = 'core/drupal.dialog.ajax';
    $response
      ->setAttachments($main_content['#attached']);

    // If the main content doesn't provide a title, use the title resolver.
    $title = isset($main_content['#title']) ? $main_content['#title'] : $this->titleResolver
      ->getTitle($request, $route_match
      ->getRouteObject());

    // Determine the title: use the title provided by the main content if any,
    // otherwise get it from the routing information.
    $options = $request->request
      ->get('dialogOptions', []);

    // Override width option.
    switch ($this->mode) {
      case 'wide':
        $options['width'] = 700;
        break;
      case 'extra_wide':
        $options['width'] = 1000;
        break;
    }
    $response
      ->addCommand(new OpenModalDialogCommand($title, $content, $options));
    return $response;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DialogRenderer::$renderer protected property The renderer. 1
DialogRenderer::$titleResolver protected property The title resolver.
DialogRenderer::determineTargetSelector protected function Determine the target selector for the OpenDialogCommand.
WideModalRenderer::$mode protected property The mode, either 'wide' or 'extra_wide'.
WideModalRenderer::renderResponse public function Renders the main content render array into a response. Overrides ModalRenderer::renderResponse
WideModalRenderer::__construct public function Constructs a new WideModalRenderer. Overrides DialogRenderer::__construct