Incorrect field object type for entity_reference type field

Created on 29 February 2024, 7 months ago
Updated 10 April 2024, 6 months ago

Problem/Motivation

Hello,

I see entity_reference field for the mocked entity returns object of class Drupal\Core\Field\FieldItemList instead of object of Drupal\Core\Field\EntityReferenceFieldItemList.

Can this be fixed somehow? or do we need stub class for EntityReferenceFieldItemList ?

Steps to reproduce

Example:
Class contains a condition which fails to execute (actual code):-
if ($field instanceof Drupal\Core\Field\EntityReferenceFieldItemList)

Unit test entity:

$fieldReferenceDefinition = FieldItemListStubFactory::createFieldItemDefinitionStub(EntityReferenceItem::class, ['target_type' => 'node']);

TestHelpers::getEntityStorage(BlockContent::class);
$entity = TestHelpers::createEntity(
  BlockContent::class,
  [
    'info' => 'Block',
    'id' => '1',
    'type' => 'bundle',
    'langcode' => 'en',
    'field_title' => 'Test block',
    'field_reference' => [
      ['target_id' => 100],
      ['target_id' => 101],
    ],
  ],
  NULL,
  [
    'fields' => [
      'field_title' => ['translatable' => TRUE],
      'field_reference' => $fieldReferenceDefinition,
    ],
  ]
);
$entity->save();
🐛 Bug report
Status

Fixed

Version

1.4

Component

Code

Created by

🇮🇳India gaurav_manerkar Vasco Da Gama, Goa

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

Merge Requests

Comments & Activities

Production build 0.71.5 2024