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.
I have this on an entity
function baseFieldDefinitions() {
$fields['channels'] = BaseFieldDefinition::create('dynamic_entity_reference')
...
->setCardinality(FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED)
->setReadOnly(TRUE);
}
function foo() {
$this->channels->appendItem($entity);
}
Using foo() will cause this fatal:
( ! ) Fatal error: Cannot use object of type Drupal\courier\Entity\Email as array in [...]/modules/dynamic_entity_reference/src/Plugin/Field/FieldType/DynamicEntityReferenceItem.php on line 340
DynamicEntityReferenceItem::setValue()
expects an array, instead of allowing an entity. This is different behavior to EntityReferenceItem::setValue()
(core ER), which does not have the code block that I have remove in the patch.
Comment | File | Size | Author |
---|---|---|---|
#7 | deritem_expects_an-2473931-7.patch | 2.81 KB | jibran |
#7 | deritem_expects_an-2473931-7-test-only.patch | 1.54 KB | jibran |
Comments
Comment #2
dpiComment #3
jibranNice catch. We just can't just remove that hunk it's there for a reason. :)
We do have tests for this DynamicEntityReferenceItemTest::testContentEntityReferenceItem()
I'd suggest to add tests for your case in this same method and then we can move forward from there.
Comment #4
dpiIf you're passing an entity, we dont need to check if it has entitytypeid. The patch was meant as a demonstration, the code block could be moved into the first
else
block.I would love to update tests but a lot of them are failing locally, unlike testbot. Is there something I need to do?
Comment #6
jibranI think you are calling it with wrong parameters.
Try
DynamicEntityReferenceItem::setValue()
always take an array either with'entity'
key or with'target_id'
and'target_type'
keys.I have added a test for above case please see 7c6c05d.
Comment #7
jibranAfter reading more code I think it's a valid bug.
Comment #9
larowlanlooks good to me - I think the original reason was DER had less features than ER cause simplicity.
But that ship has sailed - we are near feature parity.
Comment #11
jibranThanks for the review.