Corresponding entity reference using same field and same bundle results in unexpected behavior

Created on 3 October 2019, about 5 years ago
Updated 20 December 2022, almost 2 years ago

When creating a corresponding entity reference to the same bundle through the same field, CER actually creates a corresponding entity reference in every entity reference field in the bundle rather than just the specified one.

Example stucture:

Content type 1

  • field_ent_ref_to_content_type_1
  • field_ent_ref_to_content_type_2
  • field_ent_ref_to_content_type_3

Content type 2

  • field_ent_ref_to_content_type_1
  • field_ent_ref_to_content_type_2
  • field_ent_ref_to_content_type_3

Content type 3

  • field_ent_ref_to_content_type_1
  • field_ent_ref_to_content_type_2
  • field_ent_ref_to_content_type_3

Example CER configuration

This kind of CER configuration (same field and bundle) results in the problematic behavior:

Label: "Content type 1 : Content type 1"
First field: field_ent_ref_to_content_type_1
Second field: field_ent_ref_to_content_type_1
Bundles:

  • node: content_type_1

This kind of CER configuration (different fields and bundles) works fine:

Label: "Content type 1 : Content type 2"
First field: field_ent_ref_to_content_type_1
Second field: field_ent_ref_to_content_type_2
Bundles:

  • node: content_type_1
  • node: content_type_2

Behavior

In the above structure, creating a new node of Content type 1 or referencing an existing node of Content type 1 in field_ent_ref_to_content_type_1 results in a corresponding reference to Content type 1 being created in field_ent_ref_to_content_type_1 and field_ent_ref_to_content_type_2 and field_ent_ref_to_content_type_3 rather than just in field_ent_ref_to_content_type_1.

The result is that you can't reference a new or existing node of the same content type and have CER create a corresponding reference back. You get corresponding references back in every entity reference field rather than just the target entity reference field.

🐛 Bug report
Status

Active

Version

5.0

Component

Code

Created by

🇺🇸United States James Marks

Live updates comments and jobs are added and updated live.
  • Needs tests

    The change is currently missing an automated test that fails when run with the original code, and succeeds when the bug has been fixed.

Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇸🇮Slovenia jurecop

    I solved this using the hook_cer_differences_alter, by filtering entities by field bundle:

    hook_cer_differences_alter(\Drupal\Core\Entity\ContentEntityInterface $entity,
    array &$differences, $correspondingField
    ): void {
    $allowedFieldBundles = [
    'field_bundle_type' => 'bundle_type',
    ];

    if (!empty($differences['add'])) {
    $hasKey = array_key_exists($correspondingField, $allowedFieldBundles);
    if ($entity instanceof \Drupal\node\NodeInterface && $hasKey) {
    $entityType = $entity->bundle();
    if ($allowedFieldBundles[$correspondingField] !== $entityType) {
    $differences = ['add' => [], 'remove' => []];
    }
    }
    }
    }

Production build 0.71.5 2024