Migrating reference fields: target_bundles may never be empty array

Created on 16 February 2021, about 4 years ago
Updated 20 September 2024, 5 months ago

Problem/Motivation

Discovered via #2814953: Migrate Drupal 7 node/user reference fields β†’ , but is a long pre-existing bug.

#2814953-96: Migrate Drupal 7 node/user reference fields β†’ contains:

+++ b/core/modules/field/src/Plugin/migrate/process/d7/FieldInstanceSettings.php
@@ -65,6 +65,43 @@ public function transform($value, MigrateExecutableInterface $migrate_executable
+        'target_bundles' => array_filter($field_data['settings']['referenceable_types'] ?? []),

Yet … field_field_config_presave() does this:

  // In case we removed all the target bundles allowed by the field in
  // EntityReferenceItem::onDependencyRemoval() or field_entity_bundle_delete()
  // we have to log a critical message because the field will not function
  // correctly anymore.
  $handler_settings = $field->getSetting('handler_settings');
  if (isset($handler_settings['target_bundles']) && $handler_settings['target_bundles'] === []) {
    \Drupal::logger('entity_reference')->critical('The %field_name entity reference field (entity_type: %entity_type, bundle: %bundle) no longer has any valid bundle it can reference. The field is not working correctly anymore and has to be adjusted.', [
      '%field_name' => $field->getName(),
      '%entity_type' => $field->getTargetEntityTypeId(),
      '%bundle' => $field->getTargetBundle(),
    ]);
  }

IOW, target_bundles must either be NULL or if it's an array, it must be non-empty. Otherwise all references will be refused!

Steps to reproduce

node_reference field without referenceable_types setting triggers something like this during the migration:

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

πŸ› Bug report
Status

Needs work

Version

11.0 πŸ”₯

Component
MigrationΒ  β†’

Last updated 2 days ago

Created by

πŸ‡§πŸ‡ͺBelgium wim leers Ghent πŸ‡§πŸ‡ͺπŸ‡ͺπŸ‡Ί

Live updates comments and jobs are added and updated live.
  • Needs issue summary update

    Issue summaries save everyone time if they are kept up-to-date. See Update issue summary task instructions.

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.

Production build 0.71.5 2024