Verified Commit 4b835fad authored by Alex Pott's avatar Alex Pott
Browse files

Issue #2663316 by omkar.podey, jofitz, alexpott, shashank5563,...

Issue #2663316 by omkar.podey, jofitz, alexpott, shashank5563, samuel.mortenson, Andrej Galuf, djsagar, silverham, juliaschwarz, lauriii, super_romeo, gawaksh, smustgrave, sakthi_dev, Pavan B S, lammensj, Wim Leers, camerongreen, quietone: Broken title in modal dialog when title is a render array

(cherry picked from commit 4add05d0)
parent 28352d32
Loading
Loading
Loading
Loading
Loading
+3 −2
Original line number Diff line number Diff line
@@ -59,7 +59,7 @@ class OpenDialogCommand implements CommandInterface, CommandWithAttachedAssetsIn
   *
   * @param string $selector
   *   The selector of the dialog.
   * @param string $title
   * @param string|\Stringable|null $title
   *   The title of the dialog.
   * @param string|array $content
   *   The content that will be placed in the dialog, either a render array
@@ -72,8 +72,9 @@ class OpenDialogCommand implements CommandInterface, CommandWithAttachedAssetsIn
   *   on the content of the dialog. If left empty, the settings will be
   *   populated automatically from the current request.
   */
  public function __construct($selector, $title, $content, array $dialog_options = [], $settings = NULL) {
  public function __construct($selector, string|\Stringable|null $title, $content, array $dialog_options = [], $settings = NULL) {
    $title = PlainTextOutput::renderFromHtml($title);

    $dialog_options += ['title' => $title];
    $this->selector = $selector;
    $this->content = $content;
+2 −2
Original line number Diff line number Diff line
@@ -17,7 +17,7 @@ class OpenModalDialogCommand extends OpenDialogCommand {
   * until the modal has been completed. Drupal provides a built-in modal for
   * this purpose, so no selector needs to be provided.
   *
   * @param string $title
   * @param string|\Stringable|null $title
   *   The title of the dialog.
   * @param string|array $content
   *   The content that will be placed in the dialog, either a render array
@@ -30,7 +30,7 @@ class OpenModalDialogCommand extends OpenDialogCommand {
   *   on the content of the dialog. If left empty, the settings will be
   *   populated automatically from the current request.
   */
  public function __construct($title, $content, array $dialog_options = [], $settings = NULL) {
  public function __construct(string|\Stringable|null $title, $content, array $dialog_options = [], $settings = NULL) {
    $dialog_options['modal'] = TRUE;
    parent::__construct('#drupal-modal', $title, $content, $dialog_options, $settings);
  }
+2 −2
Original line number Diff line number Diff line
@@ -22,7 +22,7 @@ class OpenOffCanvasDialogCommand extends OpenDialogCommand {
   * behaviors. Drupal provides a built-in off-canvas dialog for this purpose,
   * so the selector is hard-coded in the call to the parent constructor.
   *
   * @param string $title
   * @param string|\Stringable|null $title
   *   The title of the dialog.
   * @param string|array $content
   *   The content that will be placed in the dialog, either a render array
@@ -37,7 +37,7 @@ class OpenOffCanvasDialogCommand extends OpenDialogCommand {
   * @param string $position
   *   (optional) The position to render the off-canvas dialog.
   */
  public function __construct($title, $content, array $dialog_options = [], $settings = NULL, $position = 'side') {
  public function __construct(string|\Stringable|null $title, $content, array $dialog_options = [], $settings = NULL, $position = 'side') {
    parent::__construct('#drupal-off-canvas', $title, $content, $dialog_options, $settings);
    $this->dialogOptions['modal'] = FALSE;
    $this->dialogOptions['autoResize'] = FALSE;
+34 −3
Original line number Diff line number Diff line
@@ -56,9 +56,8 @@ public function renderResponse(array $main_content, Request $request, RouteMatch
    $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 = $main_content['#title'] ?? $this->titleResolver->getTitle($request, $route_match->getRouteObject());
    // Determine the title.
    $title = $this->getTitleAsStringable($main_content, $request, $route_match);

    // Determine the dialog options and the target for the OpenDialogCommand.
    $options = $this->getDialogOptions($request);
@@ -116,4 +115,36 @@ protected function getDialogOptions(Request $request): array {
    return $request->request->all('dialogOptions');
  }

  /**
   * Gets the title as a string or stringable object.
   *
   * Uses the title provided by the main content if any, otherwise gets it from
   * the routing information.
   *
   * @param array $main_content
   *   The main content array.
   * @param \Symfony\Component\HttpFoundation\Request $request
   *   The request.
   * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
   *   The route match.
   *
   * @return \Stringable|string|null
   *   The title as a string or stringable object.
   */
  protected function getTitleAsStringable(array $main_content, Request $request, RouteMatchInterface $route_match): \Stringable|string|null {
    $title = NULL;
    if (array_key_exists('#title', $main_content)) {
      if (is_array($main_content['#title'])) {
        $title = $this->renderer->renderInIsolation($main_content['#title']);
      }
      else {
        $title = $main_content['#title'];
      }
    }
    elseif ($this->titleResolver->getTitle($request, $route_match->getRouteObject())) {
      $title = $this->titleResolver->getTitle($request, $route_match->getRouteObject())->render();
    }
    return $title;
  }

}
+2 −2
Original line number Diff line number Diff line
@@ -26,8 +26,8 @@ public function renderResponse(array $main_content, Request $request, RouteMatch
    $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 = $main_content['#title'] ?? $this->titleResolver->getTitle($request, $route_match->getRouteObject());
    // Determine the title.
    $title = $this->getTitleAsStringable($main_content, $request, $route_match);

    // Determine the dialog options for the OpenDialogCommand.
    $options = $this->getDialogOptions($request);
Loading