class EntityBrowserForm in Entity Browser 8
Same name and namespace in other branches
- 8.2 src/Form/EntityBrowserForm.php \Drupal\entity_browser\Form\EntityBrowserForm
The entity browser form.
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\entity_browser\Form\EntityBrowserForm implements BaseFormIdInterface, EntityBrowserFormInterface
Expanded class hierarchy of EntityBrowserForm
File
- src/
Form/ EntityBrowserForm.php, line 21
Namespace
Drupal\entity_browser\FormView source
class EntityBrowserForm extends FormBase implements EntityBrowserFormInterface, BaseFormIdInterface {
/**
* UUID generator service.
*
* @var \Drupal\Component\Uuid\UuidInterface
*/
protected $uuidGenerator;
/**
* The entity browser object.
*
* @var \Drupal\entity_browser\EntityBrowserInterface
*/
protected $entityBrowser;
/**
* The entity browser selection storage.
*
* @var \Drupal\Core\KeyValueStore\KeyValueStoreExpirableInterface
*/
protected $selectionStorage;
/**
* The renderer service.
*
* @var \Drupal\Core\Render\RendererInterface
*/
protected $renderer;
/**
* Constructs a EntityBrowserForm object.
*
* @param \Drupal\Component\Uuid\UuidInterface $uuid_generator
* The UUID generator service.
* @param \Drupal\Core\KeyValueStore\KeyValueStoreExpirableInterface $selection_storage
* Selection storage.
* @param \Drupal\Core\Render\RendererInterface $renderer
* The renderer service.
* @param \Drupal\Core\Messenger\MessengerInterface $messenger
* The messenger.
*/
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;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container
->get('uuid'), $container
->get('entity_browser.selection_storage'), $container
->get('renderer'), $container
->get('messenger'));
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'entity_browser_' . $this->entityBrowser
->id() . '_form';
}
/**
* {@inheritdoc}
*/
public function getBaseFormId() {
return 'entity_browser_form';
}
/**
* {@inheritdoc}
*/
public function setEntityBrowser(EntityBrowserInterface $entity_browser) {
$this->entityBrowser = $entity_browser;
}
/**
* {@inheritdoc}
*/
public function getEntityBrowser() {
return $this->entityBrowser;
}
/**
* Initializes form state.
*
* @param \Drupal\Core\Form\FormStateInterface $form_state
* Form state object.
*/
protected function init(FormStateInterface $form_state) {
// Flag that this form has been initialized.
$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);
// Initialize form state with persistent data, if present.
if ($storage = $this->selectionStorage
->get($form_state
->get([
'entity_browser',
'instance_uuid',
]))) {
foreach ($storage as $key => $value) {
$form_state
->set([
'entity_browser',
$key,
], $value);
}
}
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
// During the initial form build, add this form object to the form state and
// allow for initial preparation before form building and processing.
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.'));
}
// Add cache access cache metadata from the widgets to the form directly as
// it is affected.
foreach ($this->entityBrowser
->getWidgets() as $widget) {
/** @var \Drupal\entity_browser\WidgetInterface $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;
}
/**
* Check if entity browser with selected configuration combination can work.
*/
protected function isFunctionalForm() {
/** @var \Drupal\entity_browser\WidgetInterface $widget */
foreach ($this->entityBrowser
->getWidgets() as $widget) {
/** @var \Drupal\entity_browser\SelectionDisplayInterface $selectionDisplay */
$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.');
}
}
}
/**
* {@inheritdoc}
*/
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);
}
/**
* {@inheritdoc}
*/
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);
}
// Only call widget submit if we didn't change the widget.
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));
}
}
/**
* Returns the widget that is currently selected.
*
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
*
* @return string
* ID of currently selected widget.
*/
protected function getCurrentWidget(FormStateInterface $form_state) {
// Do not use has() as that returns TRUE if the value is NULL.
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');
}
/**
* Sets widget that is currently active.
*
* @param string $widget
* New active widget UUID.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* Form state.
*/
protected function setCurrentWidget($widget, FormStateInterface $form_state) {
$form_state
->set('entity_browser_current_widget', $widget);
}
/**
* Indicates selection is done.
*
* @param \Drupal\Core\Form\FormStateInterface $form_state
* Form state.
*
* @return bool
* Indicates selection is done.
*/
protected function isSelectionCompleted(FormStateInterface $form_state) {
return (bool) $form_state
->get([
'entity_browser',
'selection_completed',
]);
}
/**
* Returns currently selected entities.
*
* @param \Drupal\Core\Form\FormStateInterface $form_state
* Form state.
*
* @return \Drupal\Core\Entity\EntityInterface[]
* Array of currently selected entities.
*/
protected function getSelectedEntities(FormStateInterface $form_state) {
return $form_state
->get([
'entity_browser',
'selected_entities',
]);
}
}
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 | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
EntityBrowserForm:: |
protected | property | The entity browser object. | |
EntityBrowserForm:: |
protected | property | The renderer service. | |
EntityBrowserForm:: |
protected | property | The entity browser selection storage. | |
EntityBrowserForm:: |
protected | property | UUID generator service. | |
EntityBrowserForm:: |
public | function |
Form constructor. Overrides FormInterface:: |
|
EntityBrowserForm:: |
public static | function |
Instantiates a new instance of this class. Overrides FormBase:: |
|
EntityBrowserForm:: |
public | function |
Returns a string identifying the base form. Overrides BaseFormIdInterface:: |
|
EntityBrowserForm:: |
protected | function | Returns the widget that is currently selected. | |
EntityBrowserForm:: |
public | function |
Returns the entity browser entity. Overrides EntityBrowserFormInterface:: |
|
EntityBrowserForm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
|
EntityBrowserForm:: |
protected | function | Returns currently selected entities. | |
EntityBrowserForm:: |
protected | function | Initializes form state. | |
EntityBrowserForm:: |
protected | function | Check if entity browser with selected configuration combination can work. | |
EntityBrowserForm:: |
protected | function | Indicates selection is done. | |
EntityBrowserForm:: |
protected | function | Sets widget that is currently active. | |
EntityBrowserForm:: |
public | function |
Sets entity browser entity. Overrides EntityBrowserFormInterface:: |
|
EntityBrowserForm:: |
public | function |
Form submission handler. Overrides FormInterface:: |
|
EntityBrowserForm:: |
public | function |
Form validation handler. Overrides FormBase:: |
|
EntityBrowserForm:: |
public | function | Constructs a EntityBrowserForm object. | |
FormBase:: |
protected | property | The config factory. | 1 |
FormBase:: |
protected | property | The request stack. | 1 |
FormBase:: |
protected | property | The route match. | |
FormBase:: |
protected | function | Retrieves a configuration object. | |
FormBase:: |
protected | function | Gets the config factory for this form. | 1 |
FormBase:: |
private | function | Returns the service container. | |
FormBase:: |
protected | function | Gets the current user. | |
FormBase:: |
protected | function | Gets the request object. | |
FormBase:: |
protected | function | Gets the route match. | |
FormBase:: |
protected | function | Gets the logger for a specific channel. | |
FormBase:: |
protected | function |
Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait:: |
|
FormBase:: |
public | function | Resets the configuration factory. | |
FormBase:: |
public | function | Sets the config factory for this form. | |
FormBase:: |
public | function | Sets the request stack object to use. | |
LinkGeneratorTrait:: |
protected | property | The link generator. | 1 |
LinkGeneratorTrait:: |
protected | function | Returns the link generator. | |
LinkGeneratorTrait:: |
protected | function | Renders a link to a route given a route name and its parameters. | |
LinkGeneratorTrait:: |
public | function | Sets the link generator service. | |
LoggerChannelTrait:: |
protected | property | The logger channel factory service. | |
LoggerChannelTrait:: |
protected | function | Gets the logger for a specific channel. | |
LoggerChannelTrait:: |
public | function | Injects the logger channel factory. | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
RedirectDestinationTrait:: |
protected | property | The redirect destination service. | 1 |
RedirectDestinationTrait:: |
protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
RedirectDestinationTrait:: |
protected | function | Returns the redirect destination service. | |
RedirectDestinationTrait:: |
public | function | Sets the redirect destination service. | |
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. | |
UrlGeneratorTrait:: |
protected | property | The url generator. | |
UrlGeneratorTrait:: |
protected | function | Returns the URL generator service. | |
UrlGeneratorTrait:: |
public | function | Sets the URL generator service. | |
UrlGeneratorTrait:: |
protected | function | Generates a URL or path for a specific route based on the given parameters. |