You are here

OpenBootstrap4ModalDialogCommand.php in Bootstrap 4 Modal 8

Same filename and directory in other branches
  1. 2.x src/Ajax/OpenBootstrap4ModalDialogCommand.php

File

src/Ajax/OpenBootstrap4ModalDialogCommand.php
View source
<?php

namespace Drupal\bootstrap4_modal\Ajax;

use Drupal\Core\Ajax\OpenDialogCommand;

/**
 * Defines an AJAX command to open certain content in a dialog in a modal.
 *
 * @ingroup ajax
 */
class OpenBootstrap4ModalDialogCommand extends OpenDialogCommand {

  /**
   * Constructs an OpenModalDialog object.
   *
   * The modal dialog differs from the normal modal provided by
   * OpenDialogCommand in that a modal prevents other interactions on the page
   * 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
   *   The title of the dialog.
   * @param string|array $content
   *   The content that will be placed in the dialog, either a render array
   *   or an HTML string.
   * @param array $dialog_options
   *   (optional) Settings to be passed to the dialog implementation. Any
   *   jQuery UI option can be used. See http://api.jqueryui.com/dialog.
   * @param array|null $settings
   *   (optional) Custom settings that will be passed to the Drupal behaviors
   *   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) {
    $dialog_options['modal'] = TRUE;
    parent::__construct('#drupal-bootstrap4-modal', $title, $content, $dialog_options, $settings);
  }

  /**
   * Implements \Drupal\Core\Ajax\CommandInterface:render().
   */
  public function render() {

    // For consistency ensure the modal option is set to TRUE or FALSE.
    $this->dialogOptions['modal'] = isset($this->dialogOptions['modal']) && $this->dialogOptions['modal'];
    return [
      'command' => 'openBootstrap4Dialog',
      'selector' => $this->selector,
      'settings' => $this->settings,
      'data' => $this
        ->getRenderedContent(),
      'dialogOptions' => $this->dialogOptions,
    ];
  }

}

Classes

Namesort descending Description
OpenBootstrap4ModalDialogCommand Defines an AJAX command to open certain content in a dialog in a modal.