You are here

public function Modal::openModal in Entity Browser 8

Same name and namespace in other branches
  1. 8.2 src/Plugin/EntityBrowser/Display/Modal.php \Drupal\entity_browser\Plugin\EntityBrowser\Display\Modal::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/Modal.php, line 103

Class

Modal
Presents entity browser in an Modal.

Namespace

Drupal\entity_browser\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);
  $field_name = $triggering_element['#parents'][0];
  $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,
    ],
  ];
  if (!empty($this->configuration['height']) && is_numeric($this->configuration['height']) && $this->configuration['height'] > 90) {
    $content['#attributes']['height'] = $this->configuration['height'] - 90;
  }
  $html = $this->renderer
    ->render($content);
  $response = new AjaxResponse();
  $response
    ->addCommand(new OpenDialogCommand('#' . Html::getUniqueId($field_name . '-' . $element_name . '-dialog'), $this->configuration['link_text'], $html, [
    'width' => 'auto',
    'height' => 'auto',
    'modal' => TRUE,
    'maxWidth' => $this->configuration['width'],
    'maxHeight' => $this->configuration['height'],
    'fluid' => 1,
    'autoResize' => 0,
    'resizable' => 0,
    'classes' => [
      'ui-dialog' => 'entity-browser-modal',
    ],
  ]));
  return $response;
}