Problem/Motivation
When a primary_entity_reference field uses the Inline Entity Form widget, saving the parent entity can fail with:
TypeError: Cannot access offset of type string on string
in:
Drupal\primary_entity_reference\Plugin\Field\FieldWidget\PrimaryEntityReferenceInlineFormWidget->massageFormValues()
The failing code assumes every item returned from the parent widget is an array:
foreach ($values as $delta => $value) {
$values[$delta]['primary'] = 0;
}
But after calling parent::massageFormValues(), Inline Entity Form can return non-array sentinel/string values for some rows. On PHP 8+, assigning 'primary' on a string throws a fatal error.
Steps to reproduce
- Create a field of type
primary_entity_reference.
- Configure it to use the Inline Entity Form widget.
- Add or edit referenced entities inline.
- Save the parent entity.
Proposed resolution
Guard the primary assignments with is_array() checks, for example:
foreach ($values as $delta => $value) {
if (is_array($value)) {
$values[$delta]['primary'] = 0;
}
}
if ($primary_delta !== NULL && isset($values[$primary_delta]) && is_array($values[$primary_delta])) {
$values[$primary_delta]['primary'] = 1;
}
Remaining tasks
- Add a test covering
primary_entity_reference with the Inline Entity Form widget.
- Confirm the fix does not affect the other widgets.
- Commit the patch.
User interface changes
None.
API changes
None.
Data model changes
None.
Issue summary generated with AI assistance.
Comments
Comment #3
freelockNeeds review...
Comment #4
vinodhini.e commentedHi, Tested on Drupal 11.3.11 with the Primary Entity Reference field configured to use the Inline entity form - Primary widget.
I created and edited referenced entities inline, including adding, updating, and removing entities, and saved the parent entity multiple times.
However, I was unable to reproduce the reported TypeError described in the issue summary.
Could the issue summary be updated with more detailed reproduction steps?
Thanks.