Problem/Motivation
In Drupal 7, the entityreference module allowed to create multiple field instances of the same field. The migration here "works" but then any time an entity without any data attempts to set an entity reference value an InvalidArgumentException is thrown in ContentEntityBase.
InvalidArgumentException: Field <field name> is unknown.
A comparison between getting field definitions directly from entity_field.manager reveals that the field does in fact exist on the bundle, but getting the field definition on the entity itself fails. I found that if I reset the field definitions before the exception, then it tries to go further, but runs into a similar exception in typed_data_manager.
Furthermore this is only an issue for one of the bundles. The other bundle works fine.
Steps to reproduce (as detailed in a test-only patch):
- Add field_reference_3 field_config
- Add field_reference_3 field_config_instance to both article and forum node bundles.
- Run the appropriate migrations.
- Load node 2 (an article node) e.g.
$node = Node::load(2);
- Attempt to set an initial reference e.g.
$node->set('field_reference_3', [5]);
or$node->get('field_reference_3')->appendItem()->set('field_reference_3', [5]);
or$node->field_reference_3 = ['target_id' => 5];
- An exception is thrown and we can see that the field definition doesn't exist on the node, but does exist on field manager.
This might be why #2814963: Support Drupal 7 entity reference fields is still open/postponed even though plugins exist for it?
Proposed resolution
Reset the entity cache of the entity type that a field instance is attached to after the field instance is migrated.
Remaining tasks
1. Review proposed resolution
2. Reword test comments to accurately describe the issue and problem
User interface changes
Unknown
API changes
Unknown
Data model changes
Unknown