When computed fields return a label-like string (e.g. "Product code" instead of product_code) from FieldDefinitionInterface::getName(), the Custom Elements display edit form at /admin/structure/types/manage/{type}/ce-display has two
issues:

1. PHP Warning on page load: Trying to access array offset on null at EntityCustomElementsDisplayEditForm::buildFieldRow() line 745. This happens because $display_options is null when getComponent() can't find a component matching the
un-normalized field name.
2. Wrong config keys on save: When the form saves, the component name value (used as the config key) contains spaces or uppercase characters. On subsequent loads, the config key no longer
matches the expected machine name, creating a mismatch that persists even after correcting the value in the form — because the save handler's $remove_components logic only tracks form row names (field_name values), not the stale config keys.

Steps to reproduce

1. Create a content type with a computed field that has a label with spaces (e.g. "Course duration")
2. Add the field to the Custom Elements display
3. Visit /admin/structure/types/manage/{type}/ce-display
4. Observe the PHP warning and incorrect default value in the "Attribute / Slot name" text field

Root cause

In EntityCustomElementsDisplayEditForm::buildFieldRow(), the $field_name from $field_definition->getName() is used directly without normalization. For computed fields defined via hook_entity_base_field_info() or similar, getName() can return
the human-readable label instead of a proper machine name.

This un-normalized name is passed to getComponentNameFromFieldName() and then getComponent(), which fails to find a matching component because the stored config uses the normalized machine name.

Additionally, the #default_value for the name field accesses $display_options['name'] without a null check, causing the PHP warning when $display_options is null.

Proposed fix

Two changes in EntityCustomElementsDisplayEditForm::buildFieldRow():

1. Normalize the field name after retrieving it from the field definition — convert to lowercase and replace spaces with underscores:
$normalized_field_name = strtolower(str_replace(' ', '_', $field_name));
if ($field_name !== $normalized_field_name) {
$field_name = $normalized_field_name;
}

2. Add null-safe fallback for the name default value, using the label converted to snake_case:
'#default_value' => $display_options['name'] ?? strtolower(str_replace(' ', '_', (string) $label)),

Environment

- Custom Elements module version: 3.3.5
- Drupal core: 11.x
- PHP: 8.3

Comments

kushal bansal created an issue. See original summary.

kushal bansal’s picture

Status: Needs work » Needs review
fago’s picture

Status: Needs review » Postponed (maintainer needs more info)

I'd argue FieldDefinitionInterface::getName() has to return a name, not a label. so the bug seems to be in the computed field? Please clarify and check the implementation to be valid.