Issue #1379070 by ocsilalala, rkjha, swentel, kid_icarus, Gisle: Field with multiple select list does not allow -None- to be a default value.

8.0.x
Nathaniel Catchpole 2014-02-14 17:05:59 +00:00
parent 6c4cdee943
commit 13905ed190
3 changed files with 11 additions and 3 deletions

View File

@ -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 {

View File

@ -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);

View File

@ -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(