Problem/Motivation

I have two entity types, Foo and Bar. Foo has an inline entity form referencing Bar entities. Both have automatic entity labels. The label pattern of Foo contains the label of the referenced Bar entity. auto_entitylabel_inline_entity_label_callback() makes it so the label of the Foo entity is generated before being shown in the form widget, but it's incomplete, since the label of the Bar entity isn't being generated.

Proposed resolution

In the auto_entitylabel_inline_entity_label_callback() function, also generate labels of referenced entities.

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

dieterholvoet created an issue. See original summary.

dieterholvoet’s picture

Issue summary: View changes
Status: Active » Needs review
benstallings’s picture

Status: Needs review » Reviewed & tested by the community

Claude Code says:

Review of branch 3523418-labels-arent-generated

Problem

When Foo has an inline entity form referencing Bar, and Foo's label pattern includes Bar's label token, the inline entity form callback only generated Foo's label — not Bar's. So Bar's label token resolved to empty/placeholder, producing an incomplete label for Foo.

Solution (commit df9e10f)

The fix adds a loop over $entity->referencedEntities() that generates labels for referenced entities before generating the label for the parent entity. This ensures that when Foo's label pattern is token-replaced, Bar's label is already set in memory and the token resolves correctly.

Assessment

The approach is correct. The key insight is that token replacement operates on the in-memory entity objects, so referenced entities must have their labels set before the parent's setLabel() runs. The fix does exactly this.

A few concerns worth considering:

1. Only one level deep. referencedEntities() returns direct references only. If Bar's label pattern in turn depends on a Baz entity's label, this won't handle it. The original bug report only describes one level of nesting, so this is likely fine, but worth documenting as a known limitation.
2. Generates labels for all referenced entities, not just those that appear in the parent's label pattern. This is a minor over-generation — e.g., if Foo references Bar and Qux but only Bar appears in Foo's label pattern, Qux still gets setLabel() called. In practice this is harmless since autoLabelNeeded() guards against redundant work, and the cost is negligible.
3. The setLabel() return value is discarded for referenced entities (line 104), which is correct — we only need the side effect of setting the label in memory on the referenced entity object. Good.
4. No risk of saving referenced entities — setLabel() only mutates the in-memory entity object (sets the label field value and returns it). The actual persist happens in hook_entity_presave, not here. So this is safe.
5. Early return for non-ContentEntityInterface (line 91-93) is a nice cleanup vs. the original nested if.

Verdict

The patch is a clean, minimal fix for the reported problem. It correctly ensures referenced entity labels are generated before the parent entity's label, which is the root cause. The code is easy to follow and doesn't introduce side effects. Looks good to merge.