Generated with the help of an LLM.

Problem/Motivation

When default content is imported, Importer::toEntity() creates the entity with only its UUID, bundle and langcode, so field default values are applied. setFieldValues() then writes the imported items by delta, but never removes the items past the imported count. A multi-value field whose default value has more items than the imported entity keeps the leftover default items.

Translations have the same problem: each starts as a copy of the default translation, so a translation with fewer items keeps the extra items of the original.

Measured on 11.4.11, with an unlimited integer field whose default value is [1, 2]:

  • imported [2] is stored as [2, 2];
  • imported [1] is stored as [1, 2];
  • imported [7] is stored as [7, 2];
  • imported [7, 8, 9] is stored correctly.

A translatable field with en [1, 2, 3] and nl [9] stores nl as [9, 2, 3]. An entity reference field with default [A, B] and imported [B] stores [B, B]. The code is the same in 11.x, main and 12.0.0-beta2.

Steps to reproduce

  1. Give a content type an unlimited entity reference field (for example to terms) with default value A, B.
  2. Create a node that references only B.
  3. Export it: php core/scripts/drupal content:export node NID --with-dependencies --dir=content. The file lists only B.
  4. Import that directory (a recipe's content directory, or Importer::importContent()) on a site with the same configuration.
  5. The imported node references B, B.

Proposed resolution

In setFieldValues(), after writing the items, remove the ones the import didn't provide:

$items = $entity->get($field_name);
for ($delta = $items->count() - 1; $delta >= count($values); $delta--) {
  $items->removeItem($delta);
}

With this change applied to a copy of the class on 11.4.11, every case above stores exactly the imported items.

Out of scope: the exporter skips empty fields, so a field that was emptied on purpose still gets its default value on import.

Remaining tasks

User interface changes

None.

Introduced terminology

None.

API changes

None.

Data model changes

None.

Release notes snippet

Not needed.

Comments

robbesaelens created an issue.