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
No related merge requests found
...@@ -858,6 +858,8 @@ function form_select_options($element, $choices = NULL) { ...@@ -858,6 +858,8 @@ function form_select_options($element, $choices = NULL) {
// isset() fails in this situation. // isset() fails in this situation.
$value_valid = isset($element['#value']) || array_key_exists('#value', $element); $value_valid = isset($element['#value']) || array_key_exists('#value', $element);
$value_is_array = $value_valid && is_array($element['#value']); $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 = ''; $options = '';
foreach ($choices as $key => $choice) { foreach ($choices as $key => $choice) {
if (is_array($choice)) { if (is_array($choice)) {
...@@ -870,7 +872,8 @@ function form_select_options($element, $choices = NULL) { ...@@ -870,7 +872,8 @@ function form_select_options($element, $choices = NULL) {
} }
else { else {
$key = (string) $key; $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"'; $selected = ' selected="selected"';
} }
else { else {
......
...@@ -378,6 +378,7 @@ function testSelectListMultiple() { ...@@ -378,6 +378,7 @@ function testSelectListMultiple() {
// Display form: with no field data, nothing is selected. // Display form: with no field data, nothing is selected.
$this->drupalGet('entity_test/manage/' . $entity->id()); $this->drupalGet('entity_test/manage/' . $entity->id());
$this->assertOptionSelected("edit-card-2", '_none');
$this->assertNoOptionSelected('edit-card-2', 0); $this->assertNoOptionSelected('edit-card-2', 0);
$this->assertNoOptionSelected('edit-card-2', 1); $this->assertNoOptionSelected('edit-card-2', 1);
$this->assertNoOptionSelected('edit-card-2', 2); $this->assertNoOptionSelected('edit-card-2', 2);
......
...@@ -87,7 +87,9 @@ function testTaxonomyTermFieldMultipleVocabularies() { ...@@ -87,7 +87,9 @@ function testTaxonomyTermFieldMultipleVocabularies() {
// Submit an entity with both terms. // Submit an entity with both terms.
$this->drupalGet('entity_test/add'); $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( $edit = array(
'user_id' => mt_rand(0, 10), 'user_id' => mt_rand(0, 10),
'name' => $this->randomName(), 'name' => $this->randomName(),
...@@ -125,7 +127,9 @@ function testTaxonomyTermFieldMultipleVocabularies() { ...@@ -125,7 +127,9 @@ function testTaxonomyTermFieldMultipleVocabularies() {
// The widget should still be displayed. // The widget should still be displayed.
$this->drupalGet('entity_test/add'); $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. // Term 1 should still pass validation.
$edit = array( $edit = array(
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment