From 13905ed190e8a0dd2ed905bbf1296179dcbf315e Mon Sep 17 00:00:00 2001 From: Nathaniel Catchpole <catch@35733.no-reply.drupal.org> Date: Fri, 14 Feb 2014 17:05:59 +0000 Subject: [PATCH] Issue #1379070 by ocsilalala, rkjha, swentel, kid_icarus, Gisle: Field with multiple select list does not allow -None- to be a default value. --- core/includes/form.inc | 5 ++++- .../lib/Drupal/options/Tests/OptionsWidgetsTest.php | 1 + .../taxonomy/Tests/TermFieldMultipleVocabularyTest.php | 8 ++++++-- 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/core/includes/form.inc b/core/includes/form.inc index 528ff7153857..c33ff6fc8707 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 57d3e14326ef..41ba6327e1e2 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 @@ function testSelectListMultiple() { // 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 098286468ddc..81e82a93cf8c 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 @@ function testTaxonomyTermFieldMultipleVocabularies() { // 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 @@ function testTaxonomyTermFieldMultipleVocabularies() { // 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( -- GitLab