You are here

class DialogRenderer in Drupal 9

Same name and namespace in other branches
  1. 8 core/lib/Drupal/Core/Render/MainContent/DialogRenderer.php \Drupal\Core\Render\MainContent\DialogRenderer
  2. 10 core/lib/Drupal/Core/Render/MainContent/DialogRenderer.php \Drupal\Core\Render\MainContent\DialogRenderer

Default main content renderer for dialog requests.

Hierarchy

Expanded class hierarchy of DialogRenderer

1 string reference to 'DialogRenderer'
core.services.yml in core/core.services.yml
core/core.services.yml
1 service uses DialogRenderer
main_content_renderer.dialog in core/core.services.yml
Drupal\Core\Render\MainContent\DialogRenderer

File

core/lib/Drupal/Core/Render/MainContent/DialogRenderer.php, line 16

Namespace

Drupal\Core\Render\MainContent
View source
class DialogRenderer implements MainContentRendererInterface {

  /**
   * The title resolver.
   *
   * @var \Drupal\Core\Controller\TitleResolverInterface
   */
  protected $titleResolver;

  /**
   * The renderer.
   *
   * @var \Drupal\Core\Render\RendererInterface
   */
  protected $renderer;

  /**
   * Constructs a new DialogRenderer.
   *
   * @param \Drupal\Core\Controller\TitleResolverInterface $title_resolver
   *   The title resolver.
   * @param \Drupal\Core\Render\RendererInterface $renderer
   *   The renderer.
   */
  public function __construct(TitleResolverInterface $title_resolver, RendererInterface $renderer) {
    $this->titleResolver = $title_resolver;
    $this->renderer = $renderer;
  }

  /**
   * {@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 OpenDialogCommand and set the
    // attachments for this Ajax response.
    $main_content['#attached']['library'][] = 'core/drupal.dialog.ajax';
    $response
      ->setAttachments($main_content['#attached']);

    // Determine the title: use the title provided by the main content if any,
    // otherwise get it from the routing information.
    $title = isset($main_content['#title']) ? $main_content['#title'] : $this->titleResolver
      ->getTitle($request, $route_match
      ->getRouteObject());

    // Determine the dialog options and the target for the OpenDialogCommand.
    $options = $request->request
      ->get('dialogOptions', []);
    $target = $this
      ->determineTargetSelector($options, $route_match);
    $response
      ->addCommand(new OpenDialogCommand($target, $title, $content, $options));
    return $response;
  }

  /**
   * Determine the target selector for the OpenDialogCommand.
   *
   * @param array &$options
   *   The 'target' option, if set, is used, and then removed from $options.
   * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
   *   When no 'target' option is set in $options, $route_match is used instead
   *   to determine the target.
   *
   * @return string
   *   The target selector.
   */
  protected function determineTargetSelector(array &$options, RouteMatchInterface $route_match) {

    // Generate the target wrapper for the dialog.
    if (isset($options['target'])) {

      // If the target was nominated in the incoming options, use that.
      $target = $options['target'];

      // Ensure the target includes the #.
      if (substr($target, 0, 1) != '#') {
        $target = '#' . $target;
      }

      // This shouldn't be passed on to jQuery.ui.dialog.
      unset($options['target']);
    }
    else {

      // Generate a target based on the route id.
      $route_name = $route_match
        ->getRouteName();
      $target = '#' . Html::getUniqueId("drupal-dialog-{$route_name}");
    }
    return $target;
  }

}

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.
DialogRenderer::renderResponse public function Renders the main content render array into a response. Overrides MainContentRendererInterface::renderResponse 2
DialogRenderer::__construct public function Constructs a new DialogRenderer. 2