Migrating reference fields: target_bundles may never be empty array

Created on 16 February 2021, over 4 years ago
Updated 20 September 2024, 9 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 about 14 hours 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

Merge Requests

Comments & Activities

Not all content is available!

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

  • πŸ‡²πŸ‡½Mexico ovilla

    This is a reroll of #33 for Drupal 10.3

  • πŸ‡¦πŸ‡ΊAustralia acbramley

    This came up in BSI. It needs a reroll on to an MR against 11.x

  • First commit to issue fork.
  • Pipeline finished with Failed
    3 months ago
    Total: 97s
    #438063
  • Pipeline finished with Failed
    3 months ago
    Total: 603s
    #438110
  • First commit to issue fork.
  • πŸ‡¬πŸ‡§United Kingdom jofitz

    My commit fixes an issue in the unit test, but highlights another issue; what is the expected value of target_bundles in the 'node_reference no content types' test? My understanding is that it should be NULL, therefore we need to reinstate $target_bundles = empty($target_bundles) ? NULL : $target_bundles; while retaining the preceding else condition.

    @wim leers if you can dredge this from your memory then I would appreciate your opinion, thanks.

    (no longer needs reroll)

  • Pipeline finished with Failed
    about 19 hours ago
    Total: 1039s
    #515214
  • Pipeline finished with Failed
    about 19 hours ago
    Total: 563s
    #515227
Production build 0.71.5 2024