diff --git a/core/includes/form.inc b/core/includes/form.inc index 528ff715385..c33ff6fc870 100644 --- a/core/includes/form.inc +++ b/core/includes/form.inc @@ -858,6 +858,8 @@ function form_select_options($element, $choices = NULL) { // isset() fails in this situation. $value_valid = isset($element['#value']) || array_key_exists('#value', $element); $value_is_array = $value_valid && is_array($element['#value']); + // Check if the element is multiple select and no value has been selected. + $empty_value = (empty($element['#value']) && !empty($element['#multiple'])); $options = ''; foreach ($choices as $key => $choice) { if (is_array($choice)) { @@ -870,7 +872,8 @@ function form_select_options($element, $choices = NULL) { } else { $key = (string) $key; - if ($value_valid && (!$value_is_array && (string) $element['#value'] === $key || ($value_is_array && in_array($key, $element['#value'])))) { + $empty_choice = $empty_value && $key == '_none'; + if ($value_valid && ((!$value_is_array && (string) $element['#value'] === $key || ($value_is_array && in_array($key, $element['#value']))) || $empty_choice)) { $selected = ' selected="selected"'; } else { diff --git a/core/modules/options/lib/Drupal/options/Tests/OptionsWidgetsTest.php b/core/modules/options/lib/Drupal/options/Tests/OptionsWidgetsTest.php index 57d3e14326e..41ba6327e1e 100644 --- a/core/modules/options/lib/Drupal/options/Tests/OptionsWidgetsTest.php +++ b/core/modules/options/lib/Drupal/options/Tests/OptionsWidgetsTest.php @@ -378,6 +378,7 @@ class OptionsWidgetsTest extends FieldTestBase { // Display form: with no field data, nothing is selected. $this->drupalGet('entity_test/manage/' . $entity->id()); + $this->assertOptionSelected("edit-card-2", '_none'); $this->assertNoOptionSelected('edit-card-2', 0); $this->assertNoOptionSelected('edit-card-2', 1); $this->assertNoOptionSelected('edit-card-2', 2); diff --git a/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/TermFieldMultipleVocabularyTest.php b/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/TermFieldMultipleVocabularyTest.php index 098286468dd..81e82a93cf8 100644 --- a/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/TermFieldMultipleVocabularyTest.php +++ b/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/TermFieldMultipleVocabularyTest.php @@ -87,7 +87,9 @@ class TermFieldMultipleVocabularyTest extends TaxonomyTestBase { // Submit an entity with both terms. $this->drupalGet('entity_test/add'); - $this->assertFieldByName("{$this->field_name}[]", '', 'Widget is displayed.'); + // Just check if the widget for the select is displayed, the NULL value is + // used to ignore the value check. + $this->assertFieldByName("{$this->field_name}[]", NULL, 'Widget is displayed.'); $edit = array( 'user_id' => mt_rand(0, 10), 'name' => $this->randomName(), @@ -125,7 +127,9 @@ class TermFieldMultipleVocabularyTest extends TaxonomyTestBase { // The widget should still be displayed. $this->drupalGet('entity_test/add'); - $this->assertFieldByName("{$this->field_name}[]", '', 'Widget is still displayed.'); + // Just check if the widget for the select is displayed, the NULL value is + // used to ignore the value check. + $this->assertFieldByName("{$this->field_name}[]", NULL, 'Widget is still displayed.'); // Term 1 should still pass validation. $edit = array(