Steps to reproduce:
- Add a field of type "Boolean" to any entity type and bundle.
- Edit the display settings of that field.
- The two text fields for custom labels stay visible no matter what you select for "Output format".
This is caused by the following in \Drupal\Core\Field\Plugin\Field\FieldFormatter\BooleanFormatter::settingsForm(), which (I'm pretty sure) incorrectly hard-codes field_boolean as the field name:
$form['format'] = [
'#type' => 'select',
'#title' => $this->t('Output format'),
'#default_value' => $this->getSetting('format'),
'#options' => $formats,
];
$form['format_custom_true'] = [
'#type' => 'textfield',
'#title' => $this->t('Custom output for TRUE'),
'#default_value' => $this->getSetting('format_custom_true'),
'#states' => [
'visible' => [
'select[name="fields[field_boolean][settings_edit_form][settings][format]"]' => ['value' => 'custom'],
],
],
];
$form['format_custom_false'] = [
'#type' => 'textfield',
'#title' => $this->t('Custom output for FALSE'),
'#default_value' => $this->getSetting('format_custom_false'),
'#states' => [
'visible' => [
'select[name="fields[field_boolean][settings_edit_form][settings][format]"]' => ['value' => 'custom'],
],
],
];
| Comment | File | Size | Author |
|---|---|---|---|
| #2 | 2650994-2--boolean_formatter_states.patch | 2.35 KB | drunken monkey |
Comments
Comment #2
drunken monkeyAfter a bit of research, it seems the bug is definitely in the boolean formatter. Updating the IS.
The fix is pretty simple, once you've found out how to get the field name at that point in the code. Please see the attached patch.
Also, the only other formatter (at least in that folder) with
#statesseems to also have them wrong. In\Drupal\Core\Field\Plugin\Field\FieldFormatter\TimestampFormatter::settingsForm():However, I couldn't get this formatter to appear in the Fields UI, and it seems to be written to work with Views (the
#statesis correct for that), so I'm not 100% positive we should fix it there, too. However, I think so, since Views already has a work-around in place to correct the assumed#statesselectors, and because it's at least pretty confusing for others who want to write field formatters and look for examples in Core.Comment #3
swentel commentedHa, funny hardcoded bug :)
Re: timestamp item, it's not exposed as a field type for field ui (although we could do that one day, but not in this issue)
Note for committers: views overrides the states in \Drupal\views\Plugin\views\field\Date.
Test it manually, works great.
Comment #4
alexpottNice! I wish we had front end testing... Committed 7c90373 and pushed to 8.0.x and 8.1.x. Thanks!