diff --git a/core/lib/Drupal/Core/Entity/Plugin/EntityReferenceSelection/SelectionBase.php b/core/lib/Drupal/Core/Entity/Plugin/EntityReferenceSelection/SelectionBase.php index a595893..dc9c5e6 100644 --- a/core/lib/Drupal/Core/Entity/Plugin/EntityReferenceSelection/SelectionBase.php +++ b/core/lib/Drupal/Core/Entity/Plugin/EntityReferenceSelection/SelectionBase.php @@ -128,13 +128,25 @@ public function buildConfigurationForm(array $form, FormStateInterface $form_sta '#type' => 'checkboxes', '#title' => $this->t('Bundles'), '#options' => $bundle_options, - '#default_value' => (!empty($selection_handler_settings['target_bundles'])) ? $selection_handler_settings['target_bundles'] : array(), + '#default_value' => $selection_handler_settings['target_bundles'], '#required' => TRUE, '#size' => 6, '#multiple' => TRUE, '#element_validate' => array('_entity_reference_element_validate_filter'), - '#weight' => 0, + '#ajax' => TRUE, + '#limit_validation_errors' => array(), ); + + $form['target_bundles_update'] = array( + '#type' => 'submit', + '#value' => $this->t('Update bundles'), + '#limit_validation_errors' => array(), + '#attributes' => array( + 'class' => array('js-hide'), + ), + '#submit' => array('entity_reference_settings_ajax_submit'), + ]; + } else { $form['target_bundles'] = array( @@ -166,7 +178,6 @@ public function buildConfigurationForm(array $form, FormStateInterface $form_sta } } - $form['sort']['#weight'] = 10; $form['sort']['field'] = array( '#type' => 'select', '#title' => $this->t('Sort by'), @@ -203,13 +214,37 @@ public function buildConfigurationForm(array $form, FormStateInterface $form_sta } } + $form['auto_create'] = array( + '#type' => 'checkbox', + '#title' => $this->t("Create referenced entities if they don't already exist"), + '#default_value' => isset($this->configuration['handler_settings']['auto_create']) ? $this->configuration['handler_settings']['auto_create'] : FALSE, + ); + + if ($entity_type->hasKey('bundle')) { + $bundles = array_intersect_key($bundle_options, $selection_handler_settings['target_bundles']); + $form['auto_create_bundle'] = array( + '#type' => 'select', + '#title' => $this->t('Store new items in'), + '#options' => $bundles, + '#default_value' => isset($this->configuration['handler_settings']['auto_create_bundle']) ? $this->configuration['handler_settings']['auto_create_bundle'] : NULL, + '#access' => count($bundles) > 1, + '#states' => array( + 'visible' => array( + ':input[name="field[settings][handler_settings][auto_create]"]' => array('checked' => TRUE), + ), + ), + ); + } + return $form; } /** * {@inheritdoc} */ - public function validateConfigurationForm(array &$form, FormStateInterface $form_state) { } + public function validateConfigurationForm(array &$form, FormStateInterface $form_state) { + $form_state->unsetValue(['field', 'settings', 'handler_settings', 'target_bundles_update']); + } /** * {@inheritdoc} diff --git a/core/modules/taxonomy/src/Plugin/EntityReferenceSelection/TermSelection.php b/core/modules/taxonomy/src/Plugin/EntityReferenceSelection/TermSelection.php index 7a4d2bf..9a8d636 100644 --- a/core/modules/taxonomy/src/Plugin/EntityReferenceSelection/TermSelection.php +++ b/core/modules/taxonomy/src/Plugin/EntityReferenceSelection/TermSelection.php @@ -38,43 +38,7 @@ public function entityQueryAlter(SelectInterface $query) { */ public function buildConfigurationForm(array $form, FormStateInterface $form_state) { $form = parent::buildConfigurationForm($form, $form_state); - $form['target_bundles']['#title'] = $this->t('Vocabularies'); - $form['target_bundles']['#ajax'] = TRUE; - $form['target_bundles']['#limit_validation_errors'] = []; - - $form['target_bundles_update'] = [ - '#type' => 'submit', - '#value' => $this->t('Update bundles'), - '#limit_validation_errors' => [], - '#attributes' => [ - 'class' => ['js-hide'], - ], - '#submit' => ['entity_reference_settings_ajax_submit'], - ]; - - // @todo: Currently allow auto-create only on taxonomy terms. - $form['auto_create'] = [ - '#type' => 'checkbox', - '#title' => $this->t("Create referenced entities if they don't already exist"), - '#default_value' => isset($this->configuration['handler_settings']['auto_create']) ? $this->configuration['handler_settings']['auto_create'] : FALSE, - '#weight' => 20, - ]; - - $bundles = array_intersect_key($form['target_bundles']['#options'], $form['target_bundles']['#default_value']); - $form['auto_create_bundle'] = [ - '#type' => 'select', - '#title' => $this->t('Store new terms in'), - '#options' => $bundles, - '#default_value' => isset($this->configuration['handler_settings']['auto_create_bundle']) ? $this->configuration['handler_settings']['auto_create_bundle'] : NULL, - '#access' => count($bundles) > 1, - '#weight' => 30, - '#states' => [ - 'visible' => [ - ':input[name="field[settings][handler_settings][auto_create]"]' => ['checked' => TRUE], - ], - ], - ]; return $form; } @@ -82,13 +46,6 @@ public function buildConfigurationForm(array $form, FormStateInterface $form_sta /** * {@inheritdoc} */ - public function validateConfigurationForm(array &$form, FormStateInterface $form_state) { - $form_state->unsetValue(['field', 'settings', 'handler_settings', 'target_bundles_update']); - } - - /** - * {@inheritdoc} - */ public function getReferenceableEntities($match = NULL, $match_operator = 'CONTAINS', $limit = 0) { if ($match || $limit) { return parent::getReferenceableEntities($match , $match_operator, $limit);