Skip to content
This repository was archived by the owner on Jan 5, 2018. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/Controllers/StandalonePage.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,12 @@ public static function create(ContainerInterface $container) {
public function page() {
$browser = $this->loadBrowser();

// The original path is sometimes needed: ie for views arguments.
// The original path is sometimes needed: ie for views arguments.
if ($original_path = $this->request->get('original_path')) {
$browser->addAdditionalWidgetParameters(['path_parts' => explode('/', $original_path)]);
}

return \Drupal::formBuilder()->getForm($browser);
return $this->entityFormBuilder()->getForm($browser, 'default');
}

/**
Expand All @@ -93,6 +93,7 @@ public function title() {
* Loads entity browser object for this page.
*
* @return \Drupal\entity_browser\EntityBrowserInterface
* Loads the entity browser object
*/
protected function loadBrowser() {
/** @var $route \Symfony\Component\Routing\Route */
Expand Down
91 changes: 14 additions & 77 deletions src/Entity/EntityBrowser.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,18 @@
use Drupal\entity_browser\WidgetsCollection;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Routing\Route;
use Drupal\entity_browser\DisplayAjaxInterface;

/**
* Defines an entity browser configuration entity.
*
* @ConfigEntityType(
* id = "entity_browser",
* label = @Translation("Entity browser"),
* handlers = {
* "form" = {
* "default" = "Drupal\entity_browser\EntityBrowserForm"
* }
* },
* admin_permission = "administer entity browsers",
* config_prefix = "browser",
* entity_keys = {
Expand Down Expand Up @@ -449,80 +453,13 @@ public function selectionCompleted(SelectionDoneEvent $event) {
}

/**
* {@inheritdoc}
*/
public function getFormId() {
return 'entity_browser_' . $this->id() . '_form';
}

/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {

$form['selected_entities'] = array(
'#type' => 'value',
'#value' => array_map(function(EntityInterface $item) {return $item->id();}, $this->getSelectedEntities())
);

$form['#browser_parts'] = array(
'widget_selector' => 'widget_selector',
'widget' => 'widget',
'selection_display' => 'selection_display',
);
$this->getWidgetSelector()->setDefaultWidget($this->getCurrentWidget($form_state));
$form[$form['#browser_parts']['widget_selector']] = $this->getWidgetSelector()->getForm($form, $form_state);
$form[$form['#browser_parts']['widget']] = $this->getWidgets()->get($this->getCurrentWidget($form_state))->getForm($form, $form_state, $this->getAdditionalWidgetParameters());

$form['actions'] = [
'submit' => [
'#type' => 'submit',
'#value' => t('Select'),
],
];

$form[$form['#browser_parts']['selection_display']] = $this->getSelectionDisplay()->getForm($form, $form_state);

if ($this->getDisplay() instanceOf DisplayAjaxInterface) {
$this->getDisplay()->addAjax($form);
}

return $form;
}

/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
$this->getWidgetSelector()->validate($form, $form_state);
$this->getWidgets()->get($this->getCurrentWidget($form_state))->validate($form, $form_state);
$this->getSelectionDisplay()->validate($form, $form_state);
}

/**
* {@inheritdoc}
* Indicates selection is done.
*
* @return bool
* Indicates selection is done.
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$original_widget = $this->getCurrentWidget($form_state);
if ($new_widget = $this->getWidgetSelector()->submit($form, $form_state)) {
$this->setCurrentWidget($new_widget, $form_state);
}

// Only call widget submit if we didn't change the widget.
if ($original_widget == $this->getCurrentWidget($form_state)) {
$this->getWidgets()->get($this->getCurrentWidget($form_state))->submit($form[$form['#browser_parts']['widget']], $form, $form_state);
$this->getSelectionDisplay()->submit($form, $form_state);
}

// Save the selected entities to the form state.
$form_state->set('selected_entities', $this->getSelectedEntities());

if (!$this->selectionCompleted) {
$form_state->setRebuild();
}
else {
$this->getDisplay()->selectionCompleted($this->getSelectedEntities());
}
public function isSelectionCompleted() {
return $this->selectionCompleted;
}

/**
Expand All @@ -531,7 +468,6 @@ public function submitForm(array &$form, FormStateInterface $form_state) {
public function route() {
// TODO: Allow displays to define more than just path.
// See: https://www.drupal.org/node/2364193

$display = $this->getDisplay();
if ($display instanceof DisplayRouterInterface) {
$path = $display->path();
Expand Down Expand Up @@ -574,7 +510,7 @@ public function preSave(EntityStorageInterface $storage) {
* Prevent plugin collections from being serialized and correctly serialize
* selected entities.
*/
function __sleep() {
public function __sleep() {
// Save configuration for all plugins.
$this->widgets = $this->getWidgets()->getConfiguration();
$this->widget_selector_configuration = $this->widgetSelectorPluginCollection()->getConfiguration();
Expand Down Expand Up @@ -605,7 +541,7 @@ function __sleep() {
/**
* Re-register event listeners and load selected entities.
*/
function __wakeup() {
public function __wakeup() {
$this->subscribeEvents(\Drupal::service('event_dispatcher'));

$this->selectedEntities = [];
Expand All @@ -614,4 +550,5 @@ function __wakeup() {
}
$this->getSelectionDisplay()->setSelectedEntities($this->selectedEntities);
}

}
99 changes: 99 additions & 0 deletions src/EntityBrowserForm.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?php

/**
* @file
* Contains \Drupal\entity_browser\EntityBrowserForm.
*/

namespace Drupal\entity_browser;

use Drupal\Core\Entity\EntityForm;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\entity_browser\DisplayAjaxInterface;

/**
* The entity browser form.
*/
class EntityBrowserForm extends EntityForm {

/**
* {@inheritdoc}
*/
public function getFormId() {
return 'entity_browser_' . $this->entity->id() . '_form';
}

/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$entity_browser = $this->entity;
$form['selected_entities'] = array(
'#type' => 'value',
'#value' => array_map(function(EntityInterface $item) {return $item->id();}, $entity_browser->getSelectedEntities()),
);

$form['#browser_parts'] = array(
'widget_selector' => 'widget_selector',
'widget' => 'widget',
'selection_display' => 'selection_display',
);
$entity_browser->getWidgetSelector()->setDefaultWidget($entity_browser->getCurrentWidget($form_state));
$form[$form['#browser_parts']['widget_selector']] = $entity_browser->getWidgetSelector()->getForm($form, $form_state);
$form[$form['#browser_parts']['widget']] = $entity_browser->getWidgets()->get($entity_browser->getCurrentWidget($form_state))->getForm($form, $form_state, $entity_browser->getAdditionalWidgetParameters());

$form['actions'] = [
'submit' => [
'#type' => 'submit',
'#value' => t('Select'),
],
];

$form[$form['#browser_parts']['selection_display']] = $entity_browser->getSelectionDisplay()->getForm($form, $form_state);

if ($entity_browser->getDisplay() instanceOf DisplayAjaxInterface) {
$entity_browser->getDisplay()->addAjax($form);
}

return $form;
}

/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
$entity_browser = $this->entity;
$entity_browser->getWidgetSelector()->validate($form, $form_state);
$entity_browser->getWidgets()->get($entity_browser->getCurrentWidget($form_state))->validate($form, $form_state);
$entity_browser->getSelectionDisplay()->validate($form, $form_state);
}

/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$entity_browser = $this->entity;
$original_widget = $entity_browser->getCurrentWidget($form_state);
if ($new_widget = $entity_browser->getWidgetSelector()->submit($form, $form_state)) {
$entity_browser->setCurrentWidget($new_widget, $form_state);
}

// Only call widget submit if we didn't change the widget.
if ($original_widget == $entity_browser->getCurrentWidget($form_state)) {
$entity_browser->getWidgets()->get($entity_browser->getCurrentWidget($form_state))->submit($form[$form['#browser_parts']['widget']], $form, $form_state);
$entity_browser->getSelectionDisplay()->submit($form, $form_state);
}

// Save the selected entities to the form state.
$form_state->set('selected_entities', $entity_browser->getSelectedEntities());

if (!$entity_browser->isselectionCompleted()) {
$form_state->setRebuild();
}
else {
$entity_browser->getDisplay()->selectionCompleted($entity_browser->getSelectedEntities());
}
}

}
9 changes: 4 additions & 5 deletions src/EntityBrowserInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@
namespace Drupal\entity_browser;

use Drupal\Core\Config\Entity\ConfigEntityInterface;
use Drupal\Core\Form\FormInterface;
use Drupal\Core\Form\FormStateInterface;

/**
* Provides an interface defining an entity browser entity.
*/
interface EntityBrowserInterface extends ConfigEntityInterface, FormInterface {
interface EntityBrowserInterface extends ConfigEntityInterface {

/**
* Returns the entity browser name.
Expand Down Expand Up @@ -72,7 +71,7 @@ public function getWidgets();
* The widget ID.
*/
public function addWidget(array $configuration);

/**
* Deletes a widget from this entity browser.
*
Expand All @@ -88,7 +87,7 @@ public function deleteWidget(WidgetInterface $widget);
*
* Used when widgets configurations change, such as changing weights.
*
* @param \Drupal\Core\Form\FormStateInterface $form_state
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
*/
public function resetWidgets(FormStateInterface $form_state);
Expand Down Expand Up @@ -154,7 +153,7 @@ public function addSelectedEntities(array $entities);
/**
* Returns the widget that is currently selected.
*
* @param \Drupal\Core\Form\FormStateInterface $form_state
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
*
* @return string
Expand Down
14 changes: 11 additions & 3 deletions src/Tests/EntityBrowserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
use Drupal\simpletest\KernelTestBase;

/**
* Tests the entity_browser config entity
* Tests the entity_browser config entity.
*
* @group entity_browser
*/
Expand Down Expand Up @@ -54,6 +54,9 @@ class EntityBrowserTest extends KernelTestBase {
*/
protected $routeProvider;

/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();

Expand Down Expand Up @@ -302,11 +305,16 @@ protected function testSelectedEvent() {
$entity = $this->controller->load('dummy_widget');
$entity->getWidgets()->get($entity->getCurrentWidget($form_state))->entity = $entity;

$form = $entity->buildForm($form, new $form_state);
$entity->submitForm($form, $form_state);
$form_object = $this->container->get('entity.manager')->getFormObject($entity->getEntityTypeId(), 'default');
$form_object->setEntity($entity);
$form_state = (new FormState())->setFormState(array());

\Drupal::formBuilder()->buildForm($form_object, $form_state);
\Drupal::formBuilder()->submitForm($form_object, $form_state);

// Event should be dispatched from widget and added to list of selected entities.
$selected_entities = $entity->getSelectedEntities();
$this->assertEqual($selected_entities, [$entity], 'Expected selected entities detected.');
}

}