Problem/Motivation
Name fields allow configurable options for the title component (and similarly generational). Options prefixed with -- are intended to represent an empty selection. The default empty option is -- --, which renders correctly as <option value="_none">--</option> and is stored as an empty title value.
When using a custom empty option such as --Please select a Title (which is provided as example in the field settings UI), the form renders the first option incorrectly:
<option value="Please select a Title" selected="selected">Please select a Title</option>
When submitting a form with a name field like this, the name value can become this:
[
'title' => 'Please select a Title',
'given' => 'John',
'middle' => '',
'family' => 'Doe',
'generational' => '',
'credentials' => '',
]
The name gets then rendered as follows:
Please select a Title John Doe
The bug looks like to be in Name::normalizeSelectOptions(). When --Please select a Title is used as empty option, it receives something like the following:
[
'_none' => 'Please select a Title',
'Mr.' => 'Mr.',
'Mrs.' => 'Mrs.',
]
But the code only explicitly searches for a label that starts with '--'. It won't find it, because NameOptionService::getOptions() already has converted options starting with '--' to using the _none key. And having two options starting with '--' is not allowed: "Title options can only have one blank value assigned to it."
It is not safe to remove the logic in Name::normalizeSelectOptions() however, because the name element can also be used without NameOptionService.
Steps to reproduce
- Add a Name field to an entity type with the title component enabled as a select list (Drop-down field type).
- On the field settings form, set "Title options" to include a custom empty option, for example:
--Please select a Title - Create or edit an entity, leave the title at the default first option, fill in at least one of the other name fields (such as "Family") and save.
The saved value on the name field for the title component is now "Please select a Title".
Proposed resolution
In Name::normalizeSelectOptions(), check if one of the options already uses the "_none" key. If so, use that as the value for $empty_label and make sure that option is not added to the $normalized array.
Remaining tasks
- Review
- Merge
User interface changes
Title options configured to start with '--' would render with value="_none".
API changes
None.
Data model changes
None.
A fix and tests will follow.
Issue fork name-3590858
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #3
megachrizComment #4
megachrizComment #5
bluegeek9 commentedI'll review this and get a new release this week.
Comment #6
bluegeek9 commentedComment #8
bluegeek9 commented