When a Custom Field includes a text value that uses a 'Select list' widget, the allowed values don't appear to be available when the field is used in a view.
I have a Custom Field field_phone_type that includes two data items:
- phone_type_number is of type Telephone for the phone number
- phone_type_type is a text item using a select list widget, with the following allowed values:
office|Office
mobile|Mobile
fax|FaxWhen I add the field to a view and attempt to set the formatter settings, there is no option to select whether to display the 'Key' or the 'Label', as there is when using the formatter on "Manage Display" for an entity containing the same field.
Manage display formatter settings - works as expected

View formatter settings - display option missing

Additionally, when the view is rendered, I get the warning:
Warning: Undefined array key "key_label" in Drupal\custom_field\Plugin\CustomField\FieldFormatter\StringFormatter->formatValue() (line 91 of modules/contrib/custom_field/src/Plugin/CustomField/FieldFormatter/StringFormatter.php).
Drupal\custom_field\Plugin\CustomField\FieldFormatter\StringFormatter->formatValue(Array) (Line: 277)
Drupal\custom_field\Plugin\Field\FieldFormatter\BaseFormatter->getFormattedValues(Object, 'en') (Line: 45)
Drupal\custom_field\Plugin\Field\FieldFormatter\CustomFormatter->viewValue(Object) (Line: 232)
Drupal\custom_field\Plugin\Field\FieldFormatter\BaseFormatter->viewElements(Object, 'en') (Line: 89)
Drupal\Core\Field\FormatterBase->view(Object, 'en') (Line: 265)
Drupal\Core\Entity\Entity\EntityViewDisplay->buildMultiple(Array) (Line: 266)
Drupal\layout_builder\Entity\LayoutBuilderEntityViewDisplay->buildMultiple(Array) (Line: 256)
Drupal\views\Entity\Render\EntityFieldRenderer->buildFields(Array) (Line: 143)
Drupal\views\Entity\Render\EntityFieldRenderer->render(Object, Object) (Line: 833)
Drupal\views\Plugin\views\field\EntityField->getItems(Object) (Line: 1151)
Drupal\views\Plugin\views\field\FieldPluginBase->advancedRender(Object) (Line: 238)
template_preprocess_views_view_field(Array, 'views_view_field', Array)
...
Full stack trace attached.
| Comment | File | Size | Author |
|---|---|---|---|
| #14 | custom_field-3390698-14.patch | 7.07 KB | apmsooner |
| full-trace.txt | 4.74 KB | justcaldwell | |
| view-formatter-settings.png | 90.33 KB | justcaldwell | |
| manage-display-formatter-settings.png | 48.14 KB | justcaldwell |
Issue fork custom_field-3390698
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 #2
justcaldwellComment #3
apmsooner commentedThanks for reporting this @justcaldwell. I'm looking into it now to see if i can reproduce and therefore provide a quick fix!
Comment #4
justcaldwellThanks for the quick reply! I haven't had a moment to debug, but let me know if I can provide any more info.
Comment #5
apmsooner commentedWhere I'm at so far is that for some reason, the field_settings part of this https://git.drupalcode.org/project/custom_field/-/blob/2.0.x/src/Plugin/... is turning up empty when passed to view. So... the correct widget settings are never getting passed in.
running ksm() on the definition in the view is returning:
but in manage display page... the field_settings array is filled properly. So not sure why at this point.
Comment #6
justcaldwellI spent some time debugging today with no luck. I tried tracing things a bit further back and ended up setting a breakpoint on the CustomFieldTypeBase() constructor function (line 93). The only thing I noticed is that, each time a view is rendered, CustomFieldTypeBase gets instantiated 4 times for each custom item. The first two times the $configuration (where the widget settings live) array is empty; the next two times, the $configuration array is fully-loaded and all the widget settings, allowed values, etc. are present. Why is well over my head, but I thought it might be a clue for someone.
So unfortunately the only thing I can offer is this simple patch, which is NOT a fix for the issue, but will suppress the warning until it's sorted out.
Comment #7
apmsooner commentedThanks for testing things out. I will submit a patch that fixes the warning and also a bug I found with ajax/visibility paths in views as there are different array parents in views so the formatter plugin type is not actually working in views when changed. The field settings not getting passed to views though is an odd thing that seems like some sort of core bug. The schema is clearly defined so it's certainly over my head at the moment as well. I could possibly just always display the key_label dropdown in the formatter settings with some help text regardless of the widget type. That seems like it might have to be the only immediate solution so I'll test that out as well to make sure it checks out. I should be able to followup with a patch tonight that should hopefully resolve the issue with some workaround.
Thanks!
Comment #9
apmsooner commented@ justcaldwell - try this new patch. This will expose the field always regardless of the widget being used. I added some help text to try to explain its dependent on widget. The formatValue() should work fine as is now but let me know if you see anything new. Additionally, i modified some logic for the ajax and visibility path so when selecting a different formatter, the correct fields should be displayed.
Comment #10
apmsooner commentedComment #11
justcaldwellThanks @apmsooner! I really appreciate the quick turnaround.
Tried it out, and it worked well with in both a view and and a "standard" entity view display, so I decided to test with Layout Builder as well. As soon as I enable Layout Builder on the entity view display, I get the error below. Quite possibly a separate issue, but I haven't had a moment to dig in.
Comment #12
apmsooner commentedCool, I'll try to reproduce the layout builder issue and supplement the patch when i work it out.
Comment #13
apmsooner commentedOh, i think the getRandomOptions is just incorrectly typed. It should be returning a random string|int in the array_rand call. So it's unrelated to this issue but an important one to fix :)
Comment #14
apmsooner commented@ justcaldwell - try this one please.
Comment #15
justcaldwellThat's got it. Works everywhere I can think to test it. Thanks again @apmsooner!
Comment #17
apmsooner commentedMerged into dev. Will cut a new release soon.
Comment #18
apmsooner commented