View source
<?php
namespace Drupal\entity_browser\Form;
use Drupal\Component\Uuid\UuidInterface;
use Drupal\Core\Config\ConfigException;
use Drupal\Core\Form\BaseFormIdInterface;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\KeyValueStore\KeyValueStoreExpirableInterface;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\entity_browser\DisplayAjaxInterface;
use Drupal\entity_browser\EntityBrowserFormInterface;
use Drupal\entity_browser\EntityBrowserInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Render\RendererInterface;
class EntityBrowserForm extends FormBase implements EntityBrowserFormInterface, BaseFormIdInterface {
protected $uuidGenerator;
protected $entityBrowser;
protected $selectionStorage;
protected $renderer;
public function __construct(UuidInterface $uuid_generator, KeyValueStoreExpirableInterface $selection_storage, RendererInterface $renderer, MessengerInterface $messenger) {
$this->uuidGenerator = $uuid_generator;
$this->selectionStorage = $selection_storage;
$this->renderer = $renderer;
$this->messenger = $messenger;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('uuid'), $container
->get('entity_browser.selection_storage'), $container
->get('renderer'), $container
->get('messenger'));
}
public function getFormId() {
return 'entity_browser_' . $this->entityBrowser
->id() . '_form';
}
public function getBaseFormId() {
return 'entity_browser_form';
}
public function setEntityBrowser(EntityBrowserInterface $entity_browser) {
$this->entityBrowser = $entity_browser;
}
public function getEntityBrowser() {
return $this->entityBrowser;
}
protected function init(FormStateInterface $form_state) {
$form_state
->set('entity_form_initialized', TRUE);
if ($this
->getRequest()->query
->has('uuid')) {
$form_state
->set([
'entity_browser',
'instance_uuid',
], $this
->getRequest()->query
->get('uuid'));
}
else {
$form_state
->set([
'entity_browser',
'instance_uuid',
], $this->uuidGenerator
->generate());
}
$form_state
->set([
'entity_browser',
'selected_entities',
], []);
$form_state
->set([
'entity_browser',
'validators',
], []);
$form_state
->set([
'entity_browser',
'widget_context',
], []);
$form_state
->set([
'entity_browser',
'selection_completed',
], FALSE);
if ($storage = $this->selectionStorage
->get($form_state
->get([
'entity_browser',
'instance_uuid',
]))) {
foreach ($storage as $key => $value) {
$form_state
->set([
'entity_browser',
$key,
], $value);
}
}
}
public function buildForm(array $form, FormStateInterface $form_state) {
if (!$form_state
->has('entity_form_initialized')) {
$this
->init($form_state);
}
$this
->isFunctionalForm();
$form['#attributes']['class'][] = 'entity-browser-form';
if (!empty($form_state
->get([
'entity_browser',
'instance_uuid',
]))) {
$form['#attributes']['data-entity-browser-uuid'] = $form_state
->get([
'entity_browser',
'instance_uuid',
]);
}
$form['#browser_parts'] = [
'widget_selector' => 'widget_selector',
'widget' => 'widget',
'selection_display' => 'selection_display',
];
if (!($current_widget_id = $this
->getCurrentWidget($form_state))) {
$this->messenger
->addWarning($this
->t('No widgets are available.'));
return $form;
}
$this->entityBrowser
->getWidgetSelector()
->setDefaultWidget($current_widget_id);
$form[$form['#browser_parts']['widget_selector']] = $this->entityBrowser
->getWidgetSelector()
->getForm($form, $form_state);
$widget = $this->entityBrowser
->getWidget($current_widget_id);
if ($widget
->access()
->isAllowed()) {
$form[$form['#browser_parts']['widget']] = $widget
->getForm($form, $form_state, $this->entityBrowser
->getAdditionalWidgetParameters());
}
else {
$this->messenger
->addWarning($this
->t('Access to the widget forbidden.'));
}
foreach ($this->entityBrowser
->getWidgets() as $widget) {
$this->renderer
->addCacheableDependency($form, $widget
->access());
}
$form[$form['#browser_parts']['selection_display']] = $this->entityBrowser
->getSelectionDisplay()
->getForm($form, $form_state);
if ($this->entityBrowser
->getDisplay() instanceof DisplayAjaxInterface) {
$this->entityBrowser
->getDisplay()
->addAjax($form);
}
$form['#attached']['library'][] = 'entity_browser/entity_browser';
return $form;
}
protected function isFunctionalForm() {
foreach ($this->entityBrowser
->getWidgets() as $widget) {
$selectionDisplay = $this->entityBrowser
->getSelectionDisplay();
if ($widget
->requiresJsCommands() && !$selectionDisplay
->supportsJsCommands()) {
throw new ConfigException('Used entity browser selection display cannot work in combination with settings defined for used selection widget.');
}
}
}
public function validateForm(array &$form, FormStateInterface $form_state) {
$this->entityBrowser
->getWidgetSelector()
->validate($form, $form_state);
$this->entityBrowser
->getWidgets()
->get($this
->getCurrentWidget($form_state))
->validate($form, $form_state);
$this->entityBrowser
->getSelectionDisplay()
->validate($form, $form_state);
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$original_widget = $this
->getCurrentWidget($form_state);
if ($new_widget = $this->entityBrowser
->getWidgetSelector()
->submit($form, $form_state)) {
$this
->setCurrentWidget($new_widget, $form_state);
}
if ($original_widget == $this
->getCurrentWidget($form_state)) {
$this->entityBrowser
->getWidgets()
->get($this
->getCurrentWidget($form_state))
->submit($form[$form['#browser_parts']['widget']], $form, $form_state);
$this->entityBrowser
->getSelectionDisplay()
->submit($form, $form_state);
}
if (!$this
->isSelectionCompleted($form_state)) {
$form_state
->setRebuild();
}
else {
$this->entityBrowser
->getDisplay()
->selectionCompleted($this
->getSelectedEntities($form_state));
}
}
protected function getCurrentWidget(FormStateInterface $form_state) {
if (!$form_state
->get('entity_browser_current_widget')) {
$form_state
->set('entity_browser_current_widget', $this->entityBrowser
->getFirstWidget());
}
return $form_state
->get('entity_browser_current_widget');
}
protected function setCurrentWidget($widget, FormStateInterface $form_state) {
$form_state
->set('entity_browser_current_widget', $widget);
}
protected function isSelectionCompleted(FormStateInterface $form_state) {
return (bool) $form_state
->get([
'entity_browser',
'selection_completed',
]);
}
protected function getSelectedEntities(FormStateInterface $form_state) {
return $form_state
->get([
'entity_browser',
'selected_entities',
]);
}
}