class Modal in Entity Browser 8
Same name and namespace in other branches
- 8.2 src/Plugin/EntityBrowser/Display/Modal.php \Drupal\entity_browser\Plugin\EntityBrowser\Display\Modal
Presents entity browser in an Modal.
Plugin annotation
@EntityBrowserDisplay(
id = "modal",
label = @Translation("Modal"),
description = @Translation("Displays the entity browser in a modal window."),
uses_route = TRUE
)
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\entity_browser\DisplayBase implements ContainerFactoryPluginInterface, DisplayInterface uses PluginConfigurationFormTrait
- class \Drupal\entity_browser\Plugin\EntityBrowser\Display\IFrame implements DisplayRouterInterface
- class \Drupal\entity_browser\Plugin\EntityBrowser\Display\Modal
- class \Drupal\entity_browser\Plugin\EntityBrowser\Display\IFrame implements DisplayRouterInterface
- class \Drupal\entity_browser\DisplayBase implements ContainerFactoryPluginInterface, DisplayInterface uses PluginConfigurationFormTrait
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of Modal
File
- src/
Plugin/ EntityBrowser/ Display/ Modal.php, line 26
Namespace
Drupal\entity_browser\Plugin\EntityBrowser\DisplayView source
class Modal extends IFrame {
/**
* {@inheritdoc}
*/
public function displayEntityBrowser(array $element, FormStateInterface $form_state, array &$complete_form, array $persistent_data = []) {
DisplayBase::displayEntityBrowser($element, $form_state, $complete_form, $persistent_data);
$js_event_object = new RegisterJSCallbacks($this->configuration['entity_browser_id'], $this
->getUuid());
$js_event_object
->registerCallback('Drupal.entityBrowser.selectionCompleted');
$js_event = $this->eventDispatcher
->dispatch(Events::REGISTER_JS_CALLBACKS, $js_event_object);
$original_path = $this->currentPath
->getPath();
$data = [
'query_parameters' => [
'query' => [
'uuid' => $this
->getUuid(),
'original_path' => $original_path,
],
],
'attributes' => [
'data-uuid' => $this
->getUuid(),
],
];
$event_object = new AlterEntityBrowserDisplayData($this->configuration['entity_browser_id'], $this
->getUuid(), $this
->getPluginDefinition(), $form_state, $data);
$event = $this->eventDispatcher
->dispatch(Events::ALTER_BROWSER_DISPLAY_DATA, $event_object);
$data = $event
->getData();
return [
'#theme_wrappers' => [
'container',
],
'path' => [
'#type' => 'hidden',
'#value' => Url::fromRoute('entity_browser.' . $this->configuration['entity_browser_id'], [], $data['query_parameters'])
->toString(),
],
'open_modal' => [
'#type' => 'submit',
'#value' => $this->configuration['link_text'],
'#limit_validation_errors' => [],
'#submit' => [],
'#name' => implode('_', $element['#eb_parents']),
'#ajax' => [
'callback' => [
$this,
'openModal',
],
'event' => 'click',
],
'#executes_submit_callback' => FALSE,
'#attributes' => $data['attributes'],
'#attached' => [
'library' => [
'core/drupal.dialog.ajax',
'entity_browser/modal',
],
'drupalSettings' => [
'entity_browser' => [
$this
->getUuid() => [
'auto_open' => $this->configuration['auto_open'],
],
'modal' => [
$this
->getUuid() => [
'uuid' => $this
->getUuid(),
'js_callbacks' => $js_event
->getCallbacks(),
'original_path' => $original_path,
'auto_open' => $this->configuration['auto_open'],
],
],
],
],
],
],
];
}
/**
* Generates the content and opens the modal.
*
* @param array $form
* The form array.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The form state object.
*
* @return \Drupal\Core\Ajax\AjaxResponse
* An ajax response.
*/
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;
}
/**
* {@inheritdoc}
*/
public function __sleep() {
return [
'configuration',
];
}
/**
* {@inheritdoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$configuration = $this
->getConfiguration();
$form = parent::buildConfigurationForm($form, $form_state);
$form['width'] = [
'#type' => 'number',
'#title' => $this
->t('Width of the modal'),
'#default_value' => $configuration['width'],
'#description' => $this
->t('Empty value for responsive width.'),
];
$form['height'] = [
'#type' => 'number',
'#title' => $this
->t('Height of the modal'),
'#default_value' => $configuration['height'],
'#description' => $this
->t('Empty value for responsive height.'),
];
$form['auto_open']['#description'] = $this
->t('Will open Entity browser modal as soon as page is loaded, which might cause unwanted results. Should be used only in very specific cases such as Inline entity form integration. It is also advised not to use Entity browsers with this option enabled more than once per page.');
return $form;
}
/**
* {@inheritdoc}
*/
public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 2 | |
DisplayBase:: |
protected | property | Selected entities. | |
DisplayBase:: |
protected | property | Event dispatcher service. | |
DisplayBase:: |
protected | property | Plugin label. | |
DisplayBase:: |
protected | property | The selection storage. | |
DisplayBase:: |
protected | property | Instance UUID string. | |
DisplayBase:: |
protected | property | UUID generator interface. | |
DisplayBase:: |
public | function |
Calculates dependencies for the configured plugin. Overrides DependentPluginInterface:: |
|
DisplayBase:: |
public | function |
Gets this plugin's configuration. Overrides ConfigurableInterface:: |
|
DisplayBase:: |
public | function |
Gets the uuid for this display. Overrides DisplayInterface:: |
|
DisplayBase:: |
public | function |
Returns the display label. Overrides DisplayInterface:: |
|
DisplayBase:: |
public | function |
Indicates completed selection. Overrides DisplayInterface:: |
1 |
DisplayBase:: |
public | function |
Sets the configuration for this plugin instance. Overrides ConfigurableInterface:: |
|
DisplayBase:: |
public | function |
Sets the uuid for this display. Overrides DisplayInterface:: |
|
IFrame:: |
protected | property | The bare HTML page renderer. | |
IFrame:: |
protected | property | Current path. | |
IFrame:: |
protected | property | Current route match service. | |
IFrame:: |
protected | property | The renderer service. | |
IFrame:: |
protected | property | Current request. | |
IFrame:: |
public static | function |
Creates an instance of the plugin. Overrides DisplayBase:: |
|
IFrame:: |
public | function |
Gets default configuration for this plugin. Overrides DisplayBase:: |
|
IFrame:: |
public | function |
Gets page path. Overrides DisplayRouterInterface:: |
|
IFrame:: |
public | function | KernelEvents::RESPONSE listener. | |
IFrame:: |
public | function |
Constructs display plugin. Overrides DisplayBase:: |
|
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
Modal:: |
public | function |
Implements PluginFormInterface::buildConfigurationForm(). Overrides IFrame:: |
|
Modal:: |
public | function |
Displays entity browser. Overrides IFrame:: |
|
Modal:: |
public | function | Generates the content and opens the modal. | |
Modal:: |
public | function |
Implements PluginFormInterface::validateConfigurationForm(). Overrides IFrame:: |
|
Modal:: |
public | function |
Overrides DependencySerializationTrait:: |
|
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
PluginConfigurationFormTrait:: |
public | function | Implements PluginFormInterface::submitConfigurationForm(). | 3 |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |