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

  1. Add a Name field to an entity type with the title component enabled as a select list (Drop-down field type).
  2. On the field settings form, set "Title options" to include a custom empty option, for example: --Please select a Title
  3. 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

Command icon 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

megachriz created an issue. See original summary.

megachriz’s picture

Status: Active » Needs review
megachriz’s picture

Assigned: megachriz » Unassigned
bluegeek9’s picture

I'll review this and get a new release this week.

bluegeek9’s picture

Assigned: Unassigned » bluegeek9

bluegeek9’s picture

Assigned: bluegeek9 » Unassigned
Status: Needs review » Fixed
//www.flaticon.com/free-icons/thank-you Thank you for your contribution! Your continued support makes this project sustainable.
There are multiple ways to show appreciation for the work contributed to this project including:

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.