diff --git a/src/Element/EntityBrowserElement.php b/src/Element/EntityBrowserElement.php index 7d702ef..4808eb2 100644 --- a/src/Element/EntityBrowserElement.php +++ b/src/Element/EntityBrowserElement.php @@ -7,6 +7,7 @@ use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Render\Element\FormElement; use Drupal\entity_browser\Entity\EntityBrowser; use Drupal\Core\Entity\EntityInterface; +use Drupal\Component\Utility\NestedArray; /** * Provides an Entity Browser form element. @@ -148,10 +149,17 @@ class EntityBrowserElement extends FormElement { // Propagate selection if edit selection mode is used. $entity_browser_preselected_entities = []; if ($element['#selection_mode'] === static::SELECTION_MODE_EDIT) { - if ( $entity_browser->getSelectionDisplay()->checkPreselectionSupport() ) { + if ($entity_browser->getSelectionDisplay()->supportsPreselection()) { $entity_browser_preselected_entities = $element['#default_value']; - } else { - \Drupal::messenger()->addWarning(t('Configuration problem: the selection display does not support preselection, please check the form_display.')); + } + else { + $field_element = NestedArray::getValue($complete_form, array_slice($element['#array_parents'], 0, -1)); + $tparams = [ + '@field_name' => !empty($field_element['#title']) ? $field_element['#title'] : $element['#custom_hidden_id'], + '%selection_mode' => static::getSelectionModeOptions()[static::SELECTION_MODE_EDIT], + '@browser_link' => $entity_browser->toLink($entity_browser->label(), 'edit-form')->toString(), + ]; + \Drupal::messenger()->addWarning(t('There is a configuration problem with field "@field_name". The selection mode %selection_mode requires an entity browser with a selection display plugin that supports preselection. Either change the selection mode or update the @browser_link entity browser to use a selection display plugin that supports preselection.', $tparams)); } } diff --git a/src/Plugin/Field/FieldWidget/EntityReferenceBrowserWidget.php b/src/Plugin/Field/FieldWidget/EntityReferenceBrowserWidget.php index 1b6bbee..6fe6507 100644 --- a/src/Plugin/Field/FieldWidget/EntityReferenceBrowserWidget.php +++ b/src/Plugin/Field/FieldWidget/EntityReferenceBrowserWidget.php @@ -4,6 +4,7 @@ namespace Drupal\entity_browser\Plugin\Field\FieldWidget; use Drupal\Core\Entity\EntityInterface; use Drupal\entity_browser\Element\EntityBrowserElement; +use Drupal\entity_browser\Entity\EntityBrowser; use Symfony\Component\Validator\ConstraintViolationInterface; use Drupal\Component\Utility\Html; use Drupal\Component\Utility\NestedArray; @@ -248,10 +249,34 @@ class EntityReferenceBrowserWidget extends WidgetBase implements ContainerFactor ->settingsForm($form, $form_state); } + $element['#element_validate'] = [[get_class($this), 'validateSettingsForm']]; + return $element; } /** + * Validate the settings form. + */ + public static function validateSettingsForm($element, FormStateInterface $form_state, $form) { + + $values = NestedArray::getValue($form_state->getValues(), $element['#parents']); + + if ($values['selection_mode'] == 'selection_edit') { + /** @var \Drupal\entity_browser\Entity\EntityBrowser $entity_browser */ + $entity_browser = EntityBrowser::load($values['entity_browser']); + if ($entity_browser->getSelectionDisplay()->supportsPreselection() === FALSE) { + $tparams = [ + '%selection_mode' => EntityBrowserElement::getSelectionModeOptions()[EntityBrowserElement::SELECTION_MODE_EDIT], + '@browser_link' => $entity_browser->toLink($entity_browser->label(), 'edit-form')->toString(), + ]; + $form_state->setError($element['entity_browser']); + $form_state->setError($element['selection_mode'], t('The selection mode %selection_mode requires an entity browser with a selection display plugin that supports preselection. Either change the selection mode or update the @browser_link entity browser to use a selection display plugin that supports preselection.', $tparams)); + } + + } + } + + /** * Ajax callback that updates field widget display settings fieldset. */ public function updateSettingsAjax(array $form, FormStateInterface $form_state) { diff --git a/src/SelectionDisplayBase.php b/src/SelectionDisplayBase.php index d315b89..58d283f 100644 --- a/src/SelectionDisplayBase.php +++ b/src/SelectionDisplayBase.php @@ -3,6 +3,7 @@ namespace Drupal\entity_browser; use Drupal\Component\Utility\NestedArray; +use Drupal\Core\Config\ConfigException; use Drupal\Core\Plugin\PluginBase; use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Form\FormStateInterface; @@ -129,6 +130,15 @@ abstract class SelectionDisplayBase extends PluginBase implements SelectionDispl * {@inheritdoc} */ public function checkPreselectionSupport() { + if (!$this->getPluginDefinition()['acceptPreselection']) { + throw new ConfigException('Used entity browser selection display does not support preselection.'); + } + } + + /** + * {@inheritdoc} + */ + public function supportsPreselection() { return $this->getPluginDefinition()['acceptPreselection']; } diff --git a/src/SelectionDisplayInterface.php b/src/SelectionDisplayInterface.php index cce9264..6b4c874 100644 --- a/src/SelectionDisplayInterface.php +++ b/src/SelectionDisplayInterface.php @@ -62,13 +62,25 @@ interface SelectionDisplayInterface extends PluginInspectionInterface, Configura /** * Check does selection display support preselection. * + * If preselection is not allowed by entity browser selection display, then + * exception will be thrown. * - * @return boolean - * TRUE if preselection is supported + * @deprecated + * Use ::supportsPreselection instead + * + * @throws \Drupal\Core\Config\ConfigException */ public function checkPreselectionSupport(); /** + * Check if the plugin supports preselection. + * + * @returns bool + * Returns TRUE if preselection is supported. + */ + public function supportsPreselection(); + + /** * Returns true if selection display supports selection over javascript. * * @return bool