Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
From http://drupal.stackexchange.com/a/115143/39 (more code visible there)
Given the following code:
$node = Node::create(array('type' => '_entity_embed'));
$field = FieldDefinition::create('file');
$field->setName('_entity_embed');
$file = file_load(1);
$items = \Drupal::typedDataManager()->create($field, $file->id(), $field->getName(), $node);
The $items object contains items of type ConfigurableEntityReferenceItem not FileItem. Which then if you attempt to render this "fake" node field, it will result in the following error: Fatal error: Call to undefined method Drupal\entity_reference\ConfigurableEntityReferenceItem::isDisplayed()
.
Comments
Comment #1
Dave ReidComment #2
BerdirCan't reproduce this, the following works fine for me:
Sounds like your code ends up using the generic entity reference item class and not FileItem. I'm not sure why that would happen.
Comment #3
Dave ReidI think I figured it out. The previous call that ran this code was for an entity reference field. The second time it ran was for a file field. It appears that FieldDefinition does not like it if you try to re-use the same field name, so I simply appended a random, unique string to the end of the ->setName() method. That worked.
Comment #4
BerdirOh. Yeah, there's a object "cache" (prototypes) that reuses the same class for the same definition, but it's weird that it wouldn't check the type.. might be a bug.
Comment #5
BerdirSee getPropertyInstance(), $key. I guess that results in the same $key for your definition, but the name shouldn't even affect it and the type should be in there? strange..
Comment #6
Dave ReidRe-opening as it might be a bug.
Comment #7
fagohm, I see - this can happen when the parent entity is not given. I guess this could be easily fixed by moving to a field api specifc prototype implementation having the entity type as parameter, i.e. requiring #2268009: Make typed data object instantiation more flexible
Comment #16
geek-merlinNote that prototypes also carry translated strings in definitions. And that static cache is "old style", so not cacheability aware.
So that other issue might be related to this one.
Comment #20
larowlanIs this still an issue now that #2268009: Make typed data object instantiation more flexible is in?