Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
D
drupal
Manage
Activity
Members
Labels
Plan
Wiki
Custom issue tracker
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Model registry
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
project
drupal
Commits
1999c881
Commit
1999c881
authored
15 years ago
by
Angie Byron
Browse files
Options
Downloads
Patches
Plain Diff
#569542
by bjaspan and Mike Wacker: Auto-select single option if only one is available.
parent
590074fa
No related branches found
Branches containing commit
No related tags found
Tags containing commit
2 merge requests
!7452
Issue #1797438. HTML5 validation is preventing form submit and not fully...
,
!789
Issue #3210310: Adjust Database API to remove deprecated Drupal 9 code in Drupal 10
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
modules/field/modules/options/options.module
+13
-1
13 additions, 1 deletion
modules/field/modules/options/options.module
modules/field/modules/options/options.test
+34
-3
34 additions, 3 deletions
modules/field/modules/options/options.test
with
47 additions
and
4 deletions
modules/field/modules/options/options.module
+
13
−
1
View file @
1999c881
...
@@ -129,6 +129,7 @@ function options_buttons_elements_process($element, &$form_state, $form) {
...
@@ -129,6 +129,7 @@ function options_buttons_elements_process($element, &$form_state, $form) {
$element
[
'#value'
]
=
options_data2form
(
$element
,
$element
[
'#default_value'
],
$field
);
$element
[
'#value'
]
=
options_data2form
(
$element
,
$element
[
'#default_value'
],
$field
);
}
}
$options
=
options_options
(
$field
,
$instance
);
$options
=
options_options
(
$field
,
$instance
);
$required
=
isset
(
$element
[
'#required'
])
?
$element
[
'#required'
]
:
$instance
[
'required'
];
$multiple
=
isset
(
$element
[
'#multiple'
])
?
$element
[
'#multiple'
]
:
$field
[
'cardinality'
]
>
1
||
$field
[
'cardinality'
]
==
FIELD_CARDINALITY_UNLIMITED
;
$multiple
=
isset
(
$element
[
'#multiple'
])
?
$element
[
'#multiple'
]
:
$field
[
'cardinality'
]
>
1
||
$field
[
'cardinality'
]
==
FIELD_CARDINALITY_UNLIMITED
;
// Incoming #value is an array (checkboxes) or integer (radios).
// Incoming #value is an array (checkboxes) or integer (radios).
...
@@ -150,12 +151,23 @@ function options_buttons_elements_process($element, &$form_state, $form) {
...
@@ -150,12 +151,23 @@ function options_buttons_elements_process($element, &$form_state, $form) {
break
;
break
;
}
}
}
}
// If required and there is one option, make it the default.
if
(
$required
&&
count
(
$options
)
==
1
)
{
$keys
=
array_keys
(
$options
);
if
(
$multiple
)
{
$value
=
$keys
;
}
else
{
$value
=
$keys
[
0
];
}
}
$element
[
$field_key
]
=
array
(
$element
[
$field_key
]
=
array
(
'#type'
=>
$multiple
?
'checkboxes'
:
'radios'
,
'#type'
=>
$multiple
?
'checkboxes'
:
'radios'
,
'#title'
=>
$element
[
'#title'
],
'#title'
=>
$element
[
'#title'
],
'#description'
=>
$element
[
'#description'
],
'#description'
=>
$element
[
'#description'
],
'#required'
=>
isset
(
$element
[
'#required'
])
?
$element
[
'#required'
]
:
$instance
[
'required'
]
,
'#required'
=>
$required
,
'#multiple'
=>
$multiple
,
'#multiple'
=>
$multiple
,
'#options'
=>
$options
,
'#options'
=>
$options
,
'#default_value'
=>
$value
,
'#default_value'
=>
$value
,
...
...
This diff is collapsed.
Click to expand it.
modules/field/modules/options/options.test
+
34
−
3
View file @
1999c881
...
@@ -13,7 +13,6 @@ class OptionsWidgetsTestCase extends DrupalWebTestCase {
...
@@ -13,7 +13,6 @@ class OptionsWidgetsTestCase extends DrupalWebTestCase {
function
setUp
()
{
function
setUp
()
{
parent
::
setUp
(
'field_test'
);
parent
::
setUp
(
'field_test'
);
$this
->
list_values
=
array
(
1
=>
'One'
,
2
=>
'Two'
,
3
=>
'Three'
);
$this
->
card_1
=
array
(
$this
->
card_1
=
array
(
'field_name'
=>
'card_1'
,
'field_name'
=>
'card_1'
,
'type'
=>
'list'
,
'type'
=>
'list'
,
...
@@ -59,7 +58,7 @@ class OptionsWidgetsTestCase extends DrupalWebTestCase {
...
@@ -59,7 +58,7 @@ class OptionsWidgetsTestCase extends DrupalWebTestCase {
}
}
/**
/**
* Assert that a checkbox identified by $id is not checked.
* Assert that a checkbox identified by $id is
found but is
not checked.
*/
*/
function
assertIsNotChecked
(
$html
,
$id
)
{
function
assertIsNotChecked
(
$html
,
$id
)
{
$input
=
$this
->
getTagById
(
$html
,
$id
);
$input
=
$this
->
getTagById
(
$html
,
$id
);
...
@@ -91,7 +90,7 @@ class OptionsWidgetsTestCase extends DrupalWebTestCase {
...
@@ -91,7 +90,7 @@ class OptionsWidgetsTestCase extends DrupalWebTestCase {
}
}
/**
/**
* Assert that an <option> for value $value is not selected.
* Assert that an <option> for value $value is
found but is
not selected.
*/
*/
function
assertIsNotSelected
(
$html
,
$value
)
{
function
assertIsNotSelected
(
$html
,
$value
)
{
$input
=
$this
->
getOptionByValue
(
$html
,
$value
);
$input
=
$this
->
getOptionByValue
(
$html
,
$value
);
...
@@ -130,6 +129,15 @@ class OptionsWidgetsTestCase extends DrupalWebTestCase {
...
@@ -130,6 +129,15 @@ class OptionsWidgetsTestCase extends DrupalWebTestCase {
$this
->
assertIsNotChecked
(
$render
,
'edit-card-1-zxx-value-1'
);
$this
->
assertIsNotChecked
(
$render
,
'edit-card-1-zxx-value-1'
);
$this
->
assertIsChecked
(
$render
,
'edit-card-1-zxx-value-2'
);
$this
->
assertIsChecked
(
$render
,
'edit-card-1-zxx-value-2'
);
$this
->
assertIsNotChecked
(
$render
,
'edit-card-1-zxx-value-3'
);
$this
->
assertIsNotChecked
(
$render
,
'edit-card-1-zxx-value-3'
);
// Required radios with one option is auto-selected.
$instance
[
'required'
]
=
TRUE
;
field_update_instance
(
$instance
);
$this
->
card_1
[
'settings'
][
'allowed_values'
]
=
'99|Only allowed value for radios'
;
field_update_field
(
$this
->
card_1
);
$form
=
drupal_get_form
(
'field_test_entity_form'
,
$entity
);
$render
=
drupal_render
(
$form
);
$this
->
assertIsChecked
(
$render
,
'edit-card-1-zxx-value-99'
);
}
}
function
testCheckBoxes
()
{
function
testCheckBoxes
()
{
...
@@ -160,6 +168,16 @@ class OptionsWidgetsTestCase extends DrupalWebTestCase {
...
@@ -160,6 +168,16 @@ class OptionsWidgetsTestCase extends DrupalWebTestCase {
$this
->
assertIsNotChecked
(
$render
,
'edit-card-2-zxx-value-1'
);
$this
->
assertIsNotChecked
(
$render
,
'edit-card-2-zxx-value-1'
);
$this
->
assertIsChecked
(
$render
,
'edit-card-2-zxx-value-2'
);
$this
->
assertIsChecked
(
$render
,
'edit-card-2-zxx-value-2'
);
$this
->
assertIsChecked
(
$render
,
'edit-card-2-zxx-value-3'
);
$this
->
assertIsChecked
(
$render
,
'edit-card-2-zxx-value-3'
);
// Required checkbox with one option is auto-selected.
$instance
[
'required'
]
=
TRUE
;
field_update_instance
(
$instance
);
$this
->
card_2
[
'settings'
][
'allowed_values'
]
=
'99|Only allowed value for checkboxes'
;
field_update_field
(
$this
->
card_2
);
unset
(
$entity
->
card_2
);
$form
=
drupal_get_form
(
'field_test_entity_form'
,
$entity
);
$render
=
drupal_render
(
$form
);
$this
->
assertIsChecked
(
$render
,
'edit-card-2-zxx-value-99'
);
}
}
function
testSelectList
()
{
function
testSelectList
()
{
...
@@ -190,6 +208,19 @@ class OptionsWidgetsTestCase extends DrupalWebTestCase {
...
@@ -190,6 +208,19 @@ class OptionsWidgetsTestCase extends DrupalWebTestCase {
$this
->
assertIsSelected
(
$render
,
1
);
$this
->
assertIsSelected
(
$render
,
1
);
$this
->
assertIsSelected
(
$render
,
2
);
$this
->
assertIsSelected
(
$render
,
2
);
$this
->
assertIsNotSelected
(
$render
,
3
);
$this
->
assertIsNotSelected
(
$render
,
3
);
// A non-required select list has an empty key.
$form
=
drupal_get_form
(
'field_test_entity_form'
,
$entity
);
$this
->
assertTrue
(
isset
(
$form
[
'card_2'
][
FIELD_LANGUAGE_NONE
][
'value'
][
'#options'
][
''
]),
'A non-required select list has an empty key.'
);
// A required select list does not have an empty key.
$instance
[
'required'
]
=
TRUE
;
field_update_instance
(
$instance
);
$form
=
drupal_get_form
(
'field_test_entity_form'
,
$entity
);
$this
->
assertTrue
(
!
isset
(
$form
[
'card_2'
][
FIELD_LANGUAGE_NONE
][
'value'
][
'#options'
][
''
]),
'A required select list does not have an empty key.'
);
// We don't have to test that a required select list with one
// option is auto-selected because the browser does it for us.
}
}
/**
/**
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment