Skip to content

Commit

Permalink
Add ability to get context into entity browser widget
Browse files Browse the repository at this point in the history
  • Loading branch information
marcingy committed Mar 10, 2016
1 parent 0bbdb6b commit 6dafc34
Show file tree
Hide file tree
Showing 8 changed files with 159 additions and 35 deletions.
2 changes: 1 addition & 1 deletion modules/entity_form/entity_browser_entity_form.module
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down
6 changes: 5 additions & 1 deletion src/DisplayInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand Down
93 changes: 93 additions & 0 deletions src/Events/AlterEntityBrowserDisplayData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php

namespace Drupal\entity_browser\Events;

use Drupal\Core\Form\FormStateInterface;

/**
* Allows data for an entity browser element to be altered.
*/
class AlterEntityBrowserDisplayData extends EventBase {

/**
* Data to process.
*
* @var array
*/
protected $data;

/**
* Form state object.
*
* @var \Drupal\Core\Form\FormStateInterface
*/
protected $formState;

/**
* Plugin definition.
*
* @var array
*/
protected $pluginDefinition;

/**
* Constructs a EntitySelectionEvent object.
*
* @param string $entity_browser_id
* Entity browser ID.
* @param string $instance_uuid
* Entity browser instance UUID.
* @param array $plugin_definition
* Plugin definition.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* Form state object.
* @param array $data
* Data to process.
*/
public function __construct($entity_browser_id, $instance_uuid, array $plugin_definition, FormStateInterface $form_state, array $data) {
parent::__construct($entity_browser_id, $instance_uuid);
$this->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;
}
}
8 changes: 8 additions & 0 deletions src/Events/Events.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';

}
44 changes: 27 additions & 17 deletions src/Plugin/EntityBrowser/Display/IFrame.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@
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;
use Symfony\Component\HttpFoundation\Response;
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.
Expand Down Expand Up @@ -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'],
],
Expand Down
36 changes: 22 additions & 14 deletions src/Plugin/EntityBrowser/Display/Modal.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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',
Expand All @@ -162,17 +172,15 @@ 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' => [
'entity_browser' => [
'modal' => [
$uuid => [
'uuid' => $uuid,
'js_callbacks' => $event->getCallbacks(),
'js_callbacks' => $js_event->getCallbacks(),
'original_path' => $original_path,
],
],
Expand Down
3 changes: 2 additions & 1 deletion src/Plugin/EntityBrowser/Display/Standalone.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -24,7 +25,7 @@ class Standalone extends DisplayBase implements DisplayRouterInterface {
/**
* {@inheritdoc}
*/
public function displayEntityBrowser() {
public function displayEntityBrowser(FormStateInterface $form_state) {
// @TODO Implement it.
}

Expand Down
2 changes: 1 addition & 1 deletion src/Plugin/Field/FieldWidget/EntityReference.php
Original file line number Diff line number Diff line change
Expand Up @@ -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() => [
Expand Down

0 comments on commit 6dafc34

Please sign in to comment.