You are here

public function Bootstrap4Modal::openModal in Bootstrap 4 Modal 2.x

Same name and namespace in other branches
  1. 8 src/Plugin/EntityBrowser/Display/Bootstrap4Modal.php \Drupal\bootstrap4_modal\Plugin\EntityBrowser\Display\Bootstrap4Modal::openModal()

Generates the content and opens the modal.

Parameters

array $form: The form array.

\Drupal\Core\Form\FormStateInterface $form_state: The form state object.

Return value

\Drupal\Core\Ajax\AjaxResponse An ajax response.

File

src/Plugin/EntityBrowser/Display/Bootstrap4Modal.php, line 114

Class

Bootstrap4Modal
Presents entity browser in an Modal.

Namespace

Drupal\bootstrap4_modal\Plugin\EntityBrowser\Display

Code

public function openModal(array &$form, FormStateInterface $form_state) {
  $triggering_element = $form_state
    ->getTriggeringElement();
  $parents = $triggering_element['#parents'];
  array_pop($parents);
  $parents = array_merge($parents, [
    'path',
  ]);
  $input = $form_state
    ->getUserInput();
  $src = NestedArray::getValue($input, $parents);
  $element_name = $this->configuration['entity_browser_id'];
  $name = 'entity_browser_iframe_' . $element_name;
  $content = [
    '#prefix' => '<div class="ajax-progress-throbber"></div>',
    '#type' => 'html_tag',
    '#tag' => 'iframe',
    '#attributes' => [
      'src' => $src,
      'class' => 'entity-browser-modal-iframe',
      'width' => '100%',
      'frameborder' => 0,
      'style' => 'padding:0; position:relative; z-index:10002;',
      'name' => $name,
      'id' => $name,
    ],
  ];
  $html = $this->renderer
    ->render($content);
  $title = $this
    ->t($this->configuration['link_text']);
  $options = [
    'dialogClasses' => [],
    'options' => [
      'fluid' => TRUE,
    ],
  ];
  $dialogClasses = [];
  if (!empty($this->configuration['modal_size'])) {
    $dialogClasses[] = $this->configuration['modal_size'];
  }
  $options['dialogClasses'] = implode(' ', $dialogClasses);
  $response = new AjaxResponse();
  $response
    ->addCommand(new OpenBootstrap4ModalDialogCommand($title, $html, $options));
  return $response;
}