Problem/Motivation
The primary_entity_reference field type does not support the entity key format that Drupal's core serialization system uses during recipe and default content import. Standard entity_reference fields work with this format; primary_entity_reference fields do not.
When using Drupal Recipes or Default Content with primary_entity_reference fields, YAML like the following does not populate the field:
emails: - entity: '676cfc78-1cac-5a48-b729-fbba59d7c230'
No error is thrown, but the field remains empty after import. The same structure works for standard entity_reference fields (e.g. the contacts field on crm_relationship). This prevents recipes from importing content that uses primary_entity_reference for fields such as emails, telephones, or addresses.
Steps to reproduce
- Create a recipe that imports content entities with
primary_entity_referencebase fields (e.g. a contact entity withemails,telephones,addressesfields). - In the content YAML, reference related entities using the
entity: 'uuid'format, with_meta.dependsdeclaring the referenced entity type so import order is correct. - Apply the recipe (e.g.
drush recipe:apply path/to/recipe). - Observe: entities using standard
entity_reference(e.g. relationships) have their reference fields populated; entities usingprimary_entity_referencehave those fields empty.
Example YAML that fails to populate primary_entity_reference fields:
_meta: version: 1.0 entity_type: crm_contact uuid: '00af71f6-cff0-5716-a2b1-323288beab04' bundle: person depends: c5a34440-9a73-5685-881e-f929fe15356f: crm_contact_method c753a2f1-3c68-50db-b069-a4c5957c7f10: crm_contact_method default: name: [{ value: Lisa Simpson }] emails: - entity: 'c5a34440-9a73-5685-881e-f929fe15356f' telephones: - entity: 'c753a2f1-3c68-50db-b069-a4c5957c7f10'
Expected: emails and telephones are populated with the referenced crm_contact_method entities.
Actual: Fields remain empty; no error is reported.
Proposed resolution
Add a custom normalizer so that primary_entity_reference field items support the entity key during denormalization (YAML → PHP), matching the behavior of core's handling for EntityReferenceItem.
- Implement a normalizer class (e.g.
PrimaryEntityReferenceItemNormalizer) that extends the appropriate base (e.g.FieldItemNormalizeror the normalizer used for entity reference items). - In
denormalize(), detect theentitykey with a UUID value; load the entity by UUID (e.g. viaentity.repositoryorentity_type.manager+ storageloadByProperties(['uuid' => $uuid])); convert to thetarget_idformat the field expects; then call the parent denormalizer. - Register the normalizer in
primary_entity_reference.services.ymlwith a priority higher than the default field item normalizers so it is used forPrimaryEntityReferenceItem(or the module's field item class).
This is an API addition (new normalizer service) and is backward compatible: it only adds support for a format that currently has no effect.
Remaining tasks
- Implement the normalizer and register it in
services.yml. - Add kernel tests for denormalization of the
entitykey (UUID →target_id). - Add an integration test that applies a recipe containing content with
primary_entity_referencefields and asserts the fields are populated.
User interface changes
None.
API changes
New normalizer service for PrimaryEntityReferenceItem (or the module's field item class), used during content/recipe import. No breaking changes.
Data model changes
None.
Issue fork primary_entity_reference-3571040
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
Comment #4
bluegeek9 commented