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

  1. Create a field of type primary_entity_reference.
  2. Configure it to use the Inline Entity Form widget.
  3. Add or edit referenced entities inline.
  4. 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

  1. Add a test covering primary_entity_reference with the Inline Entity Form widget.
  2. Confirm the fix does not affect the other widgets.
  3. Commit the patch.

User interface changes

None.

API changes

None.

Data model changes

None.

Issue summary generated with AI assistance.

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

freelock created an issue. See original summary.

freelock’s picture

Status: Active » Needs review

Needs review...

vinodhini.e’s picture

Hi, 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.