Do not pass NULL for bundle to EntityFieldManager::getFieldDefinitions()

Created on 28 July 2023, over 1 year ago
Updated 18 August 2023, about 1 year ago

Problem/Motivation

When for a migration no bundle specified is specified, Migration::getDestinationFields() passes NULL to
EntityFieldManager::getFieldDefinitions() for $bundle parameter, but that should be a string.

This results into an error when there is the following custom code defined:

/**
 * Implements hook_entity_bundle_field_info_alter().
 */
function mymodule_entity_bundle_field_info_alter(array &$fields, EntityTypeInterface $entity_type, string $bundle): void {

The error is then:

TypeError: mymodule_entity_bundle_field_info_alter(): Argument #3 ($bundle) must be of type string, null given

For user migrations, specifying a bundle is not needed, because the user entity type does not support bundles.
For other entity types that do support bundles, the UI currently does require the user to specify a bundle, but existing migrations (created manually in a yaml file) are not guaranteed to have it. In these situations, EntityFieldManager::getBaseFieldDefinitions() should be called instead.

Steps to reproduce

  1. Create a user migration.
  2. Implement hook_entity_bundle_field_info_alter() in a custom module and set the type hint "string" for the $bundle parameter.

Or:

  1. In a YAML file, create a migration for node and don't specify the bundle.
  2. Implement hook_entity_bundle_field_info_alter() in a custom module and set the type hint "string" for the $bundle parameter.

Proposed resolution

In Migration::getDestinationFields() check if $entity_bundle is NULL. If so, check if the entity type supports bundles. If not, pass $entity_type_id as the bundle to EntityFieldManager::getFieldDefinitions(). Else call EntityFieldManager::getBaseFieldDefinitions() instead.

Add test coverage for above two situations.

Remaining tasks

User interface changes

API changes

Data model changes

🐛 Bug report
Status

Fixed

Version

1.0

Component

Code

Created by

🇳🇱Netherlands megachriz

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Production build 0.71.5 2024