From 6dafc34298f8c7a3e1d85e84ac4d27ae76cec226 Mon Sep 17 00:00:00 2001 From: marcingy Date: Thu, 10 Mar 2016 15:01:32 -0700 Subject: [PATCH] Add ability to get context into entity browser widget --- .../entity_browser_entity_form.module | 2 +- src/DisplayInterface.php | 6 +- src/Events/AlterEntityBrowserDisplayData.php | 93 +++++++++++++++++++ src/Events/Events.php | 8 ++ src/Plugin/EntityBrowser/Display/IFrame.php | 44 +++++---- src/Plugin/EntityBrowser/Display/Modal.php | 36 ++++--- .../EntityBrowser/Display/Standalone.php | 3 +- .../Field/FieldWidget/EntityReference.php | 2 +- 8 files changed, 159 insertions(+), 35 deletions(-) create mode 100644 src/Events/AlterEntityBrowserDisplayData.php diff --git a/modules/entity_form/entity_browser_entity_form.module b/modules/entity_form/entity_browser_entity_form.module index 41894b0..1c64ba9 100644 --- a/modules/entity_form/entity_browser_entity_form.module +++ b/modules/entity_form/entity_browser_entity_form.module @@ -38,7 +38,7 @@ function entity_browser_entity_form_inline_entity_form_reference_form_alter(&$re 'class' => ['ief-entity-browser-value'], ], ]; - $reference_form['entity_browser'] = $display->displayEntityBrowser(); + $reference_form['entity_browser'] = $display->displayEntityBrowser($form_state); $reference_form['#attached']['library'][] = 'entity_browser_entity_form/ief_autocomplete'; $reference_form['actions']['ief_reference_save']['#ajax']['event'] = 'entities-selected'; diff --git a/src/DisplayInterface.php b/src/DisplayInterface.php index 61e9521..80943de 100644 --- a/src/DisplayInterface.php +++ b/src/DisplayInterface.php @@ -10,6 +10,7 @@ use Drupal\Component\Plugin\ConfigurablePluginInterface; use Drupal\Component\Plugin\PluginInspectionInterface; use Drupal\Core\Plugin\PluginFormInterface; +use Drupal\Core\Form\FormStateInterface; /** * Defines the interface for entity browser displays. @@ -31,10 +32,13 @@ public function label(); * with it. It will take care about displaying entity browser in one way or * another. * + * @param \Drupal\Core\Form\FormStateInterface $form_state + * The form state object. + * * @return array * An array suitable for drupal_render(). */ - public function displayEntityBrowser(); + public function displayEntityBrowser(FormStateInterface $form_state); /** * Indicates completed selection. diff --git a/src/Events/AlterEntityBrowserDisplayData.php b/src/Events/AlterEntityBrowserDisplayData.php new file mode 100644 index 0000000..f4ca17c --- /dev/null +++ b/src/Events/AlterEntityBrowserDisplayData.php @@ -0,0 +1,93 @@ +data = $data; + $this->formState = $form_state; + $this->pluginDefinition = $plugin_definition; + } + + /** + * Gets form state. + * + * @return \Drupal\Core\Form\FormStateInterface + * Form state object. + */ + public function getFormState() { + return $this->formState; + } + + /** + * Gets data array. + * + * @return array + * Data array. + */ + public function getData() { + return $this->data; + } + + /** + * Gets plugin definition array. + * + * @return array + * Plugin definition array. + */ + public function getPluginDefinition() { + return $this->pluginDefinition; + } + + /** + * Sets data array. + * + * @param array $data + * Data array. + */ + public function setData($data) { + $this->data = $data; + } +} diff --git a/src/Events/Events.php b/src/Events/Events.php index 2a46a2b..b738603 100644 --- a/src/Events/Events.php +++ b/src/Events/Events.php @@ -37,4 +37,12 @@ final class Events { */ const REGISTER_JS_CALLBACKS = 'entity_browser.register_js_callbacks'; + /** + * The ALTER_BROWSER_DISPLAY_DATA allows for entity browser display plugin data + * to be tweaked. + * + * @var string + */ + const ALTER_BROWSER_DISPLAY_DATA = 'entity_browser.alter_browser_display_data'; + } diff --git a/src/Plugin/EntityBrowser/Display/IFrame.php b/src/Plugin/EntityBrowser/Display/IFrame.php index 7ffe0ef..7243143 100644 --- a/src/Plugin/EntityBrowser/Display/IFrame.php +++ b/src/Plugin/EntityBrowser/Display/IFrame.php @@ -15,6 +15,7 @@ use Drupal\entity_browser\DisplayRouterInterface; use Drupal\entity_browser\Events\Events; use Drupal\entity_browser\Events\RegisterJSCallbacks; +use Drupal\entity_browser\Events\AlterEntityBrowserDisplayData; use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Drupal\Core\Path\CurrentPathStack; @@ -22,6 +23,7 @@ use Symfony\Component\HttpKernel\Event\FilterResponseEvent; use Symfony\Component\HttpKernel\KernelEvents; use Symfony\Component\HttpFoundation\Request; +use Drupal\Core\Form\FormStateInterface; /** * Presents entity browser in an iFrame. @@ -127,43 +129,51 @@ public function defaultConfiguration() { /** * {@inheritdoc} */ - public function displayEntityBrowser() { + public function displayEntityBrowser(FormStateInterface $form_state) { $uuid = $this->getUuid(); /** @var \Drupal\entity_browser\Events\RegisterJSCallbacks $event */ // TODO - $uuid is unused in this event but we need to pass it as // constructor expects it. See https://www.drupal.org/node/2600706 for more // info. - $event_object = new RegisterJSCallbacks($this->configuration['entity_browser_id'], $uuid); - $event_object->registerCallback('Drupal.entityBrowser.selectionCompleted'); - $event = $this->eventDispatcher->dispatch(Events::REGISTER_JS_CALLBACKS, $event_object ); + $js_event_object = new RegisterJSCallbacks($this->configuration['entity_browser_id'], $uuid); + $js_event_object->registerCallback('Drupal.entityBrowser.selectionCompleted'); + $callback_event = $this->eventDispatcher->dispatch(Events::REGISTER_JS_CALLBACKS, $js_event_object); $original_path = $this->currentPath->getPath(); + $data = [ + 'query_parameters' => [ + 'query' => [ + 'uuid' => $uuid, + 'original_path' => $original_path, + ], + ], + 'attributes' => [ + 'href' => '#browser', + 'class' => ['entity-browser-handle', 'entity-browser-iframe'], + 'data-uuid' => $uuid, + 'data-original-path' => $original_path, + ], + ]; + $event_object = new AlterEntityBrowserDisplayData($this->configuration['entity_browser_id'], $uuid, $this->getPluginDefinition(), $form_state, $data); + $event = $this->eventDispatcher->dispatch(Events::ALTER_BROWSER_DISPLAY_DATA, $event_object); + $data = $event->getData(); return [ '#theme_wrappers' => ['container'], 'link' => [ '#type' => 'html_tag', '#tag' => 'a', '#value' => $this->configuration['link_text'], - '#attributes' => [ - 'href' => '#browser', - 'class' => ['entity-browser-handle', 'entity-browser-iframe'], - 'data-uuid' => $uuid, - 'data-original-path' => $original_path, - ], + '#attributes' => $data['attributes'], '#attached' => [ 'library' => ['entity_browser/iframe'], 'drupalSettings' => [ 'entity_browser' => [ 'iframe' => [ $uuid => [ - 'src' => Url::fromRoute('entity_browser.' . $this->configuration['entity_browser_id'], [], [ - 'query' => [ - 'uuid' => $uuid, - 'original_path' => $original_path, - ] - ])->toString(), + 'src' => Url::fromRoute('entity_browser.' . $this->configuration['entity_browser_id'], [], $data['query_parameters']) + ->toString(), 'width' => $this->configuration['width'], 'height' => $this->configuration['height'], - 'js_callbacks' => $event->getCallbacks(), + 'js_callbacks' => $callback_event->getCallbacks(), 'entity_browser_id' => $this->configuration['entity_browser_id'], 'auto_open' => $this->configuration['auto_open'], ], diff --git a/src/Plugin/EntityBrowser/Display/Modal.php b/src/Plugin/EntityBrowser/Display/Modal.php index 5e4e95b..8011856 100644 --- a/src/Plugin/EntityBrowser/Display/Modal.php +++ b/src/Plugin/EntityBrowser/Display/Modal.php @@ -29,6 +29,7 @@ use Drupal\Core\Form\FormStateInterface; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; +use Drupal\entity_browser\Events\AlterEntityBrowserDisplayData; /** * Presents entity browser in an Modal. @@ -131,26 +132,35 @@ public function defaultConfiguration() { /** * {@inheritdoc} */ - public function displayEntityBrowser() { + public function displayEntityBrowser(FormStateInterface $form_state) { $uuid = $this->getUuid(); /** @var \Drupal\entity_browser\Events\RegisterJSCallbacks $event */ // TODO - $uuid is unused in this event but we need to pass it as // constructor expects it. See https://www.drupal.org/node/2600706 for more // info. - $event_object = new RegisterJSCallbacks($this->configuration['entity_browser_id'], $uuid); - $event_object->registerCallback('Drupal.entityBrowser.selectionCompleted'); - $event = $this->eventDispatcher->dispatch(Events::REGISTER_JS_CALLBACKS, $event_object ); + $js_event_object = new RegisterJSCallbacks($this->configuration['entity_browser_id'], $uuid); + $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' => $uuid, + 'original_path' => $original_path, + ], + ], + 'attributes' => [ + 'data-uuid' => $uuid, + ], + ]; + $event_object = new AlterEntityBrowserDisplayData($this->configuration['entity_browser_id'], $uuid, $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'], [], [ - 'query' => [ - 'uuid' => $uuid, - 'original_path' => $original_path, - ], - ])->toString(), + '#value' => Url::fromRoute('entity_browser.' . $this->configuration['entity_browser_id'], [], $data['query_parameters'])->toString(), ], 'open_modal' => [ '#type' => 'submit', @@ -162,9 +172,7 @@ public function displayEntityBrowser() { 'callback' => [$this, 'openModal'], 'event' => 'click', ], - '#attributes' => [ - 'data-uuid' => $uuid, - ], + '#attributes' => $data['attributes'], '#attached' => [ 'library' => ['core/drupal.dialog.ajax', 'entity_browser/modal'], 'drupalSettings' => [ @@ -172,7 +180,7 @@ public function displayEntityBrowser() { 'modal' => [ $uuid => [ 'uuid' => $uuid, - 'js_callbacks' => $event->getCallbacks(), + 'js_callbacks' => $js_event->getCallbacks(), 'original_path' => $original_path, ], ], diff --git a/src/Plugin/EntityBrowser/Display/Standalone.php b/src/Plugin/EntityBrowser/Display/Standalone.php index 6ffe2cf..24beb72 100644 --- a/src/Plugin/EntityBrowser/Display/Standalone.php +++ b/src/Plugin/EntityBrowser/Display/Standalone.php @@ -8,6 +8,7 @@ use Drupal\entity_browser\DisplayBase; use Drupal\entity_browser\DisplayRouterInterface; +use Drupal\Core\Form\FormStateInterface; /** * Presents entity browser as a standalone form. @@ -24,7 +25,7 @@ class Standalone extends DisplayBase implements DisplayRouterInterface { /** * {@inheritdoc} */ - public function displayEntityBrowser() { + public function displayEntityBrowser(FormStateInterface $form_state) { // @TODO Implement it. } diff --git a/src/Plugin/Field/FieldWidget/EntityReference.php b/src/Plugin/Field/FieldWidget/EntityReference.php index 21a0e0c..dade6d2 100644 --- a/src/Plugin/Field/FieldWidget/EntityReference.php +++ b/src/Plugin/Field/FieldWidget/EntityReference.php @@ -338,7 +338,7 @@ function formElement(FieldItemListInterface $items, $delta, array $element, arra $entity_browser_uuid = sha1(implode('-', array_merge($form['#parents'], [$this->fieldDefinition->getName(), $delta]))); $entity_browser_display = $entity_browser->getDisplay(); $entity_browser_display->setUuid($entity_browser_uuid); - $element['entity_browser'] = $entity_browser_display->displayEntityBrowser(); + $element['entity_browser'] = $entity_browser_display->displayEntityBrowser($form_state); $element['#attached']['library'][] = 'entity_browser/entity_reference'; $element['#attached']['drupalSettings']['entity_browser'] = [ $entity_browser->getDisplay()->getUuid() => [