Skip to content
Snippets Groups Projects
Commit 13905ed1 authored by catch's avatar catch
Browse files

Issue #1379070 by ocsilalala, rkjha, swentel, kid_icarus, Gisle: Field with...

Issue #1379070 by ocsilalala, rkjha, swentel, kid_icarus, Gisle: Field with multiple select list does not allow -None- to be a default value.
parent 6c4cdee9
No related branches found
No related tags found
2 merge requests!7452Issue #1797438. HTML5 validation is preventing form submit and not fully...,!789Issue #3210310: Adjust Database API to remove deprecated Drupal 9 code in Drupal 10
......@@ -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 {
......
......@@ -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);
......
......@@ -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(
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment