DynamicEntityReferenceItem::getValue() and DynamicEntityReferenceItem::setValue() are throwing errors when field value is set programmatically.

Comments

Status: Needs review » Needs work

The last submitted patch, der-getvalue-not-working.patch, failed testing.

larowlan’s picture

Will check this in the morning

larowlan’s picture

Had a bit of a look into this but didn't get far

larowlan’s picture

Are you sure that getValues fails? we have tests for that

jibran’s picture

Sorry InvalidArgumentException is thrown by DynamicEntityReference::setValue()

jibran’s picture

Status: Needs work » Needs review
StatusFileSize
new7.04 KB
new1014 bytes

perhaps this will help.

Status: Needs review » Needs work

The last submitted patch, 6: der-getvalue-not-working-6.patch, failed testing.

jibran’s picture

Status: Needs work » Needs review
StatusFileSize
new8.45 KB
new1.69 KB

Here is little progress.

Status: Needs review » Needs work

The last submitted patch, 8: der-getvalue-not-working-8.patch, failed testing.

amateescu’s picture

I'll try to take a look tonight :)

jibran’s picture

Thank you very much.

amateescu’s picture

Sorry, didn't make it this evening. I'll try harder tomorrow :/

amateescu’s picture

This was the issue that's been eating up my evenings: #2370703: ER's "autocreate" feature is mostly broken (and untested)

You might want to check if the same problems exist in Dynamic ER too.

jibran’s picture

Yeah I am following that will review/test/rtbc it once green :)

jibran’s picture

StatusFileSize
new1.97 KB
new8.15 KB

I tried some stuff but no luck. This patch is use less but I am uploading it anyway.

jibran’s picture

Status: Needs work » Needs review
  1. +++ b/src/Plugin/DataType/DynamicEntityReference.php
    @@ -89,8 +89,11 @@ class DynamicEntityReference extends EntityReference {
    +    if (!isset($value)) {
    +      $this->target = NULL;
    +    }
    +    elseif ($value instanceof EntityInterface) {
    +      $this->target = $value->getTypedData();
    

    These changes are copied form EntityReference

  2. +++ b/src/Plugin/DataType/DynamicEntityReference.php
    @@ -89,8 +89,11 @@ class DynamicEntityReference extends EntityReference {
         elseif (!is_scalar($value) || $this->getTargetDefinition()->getEntityTypeId() === NULL) {
    

    so when we do

    $entity->field_test_taxonomy_term->target_id = $tid;
    $entity->field_test_taxonomy_term->target_type = $entity_type_id;

    it throws exception when we try to access $entity->field_test_taxonomy_term->entity because entity type id is not set.

  3. +++ b/src/Plugin/Field/FieldType/DynamicEntityReferenceItem.php
    @@ -55,7 +55,7 @@ class DynamicEntityReferenceItem extends ConfigurableEntityReferenceItem {
    -    $properties['target_id'] = DataDefinition::create('string')
    +    $properties['target_id'] = DataDefinition::create('integer')
    

    useless change we don't need this. But we have to update this function cuz base class has updated it.

  4. +++ b/src/Plugin/Field/FieldType/DynamicEntityReferenceItem.php
    @@ -194,7 +194,7 @@ class DynamicEntityReferenceItem extends ConfigurableEntityReferenceItem {
    +    if (!empty($values['target_type']) && empty($values['entity'])) {
    

    I dono about this either just trying some stuff.

Main pain point is $entity->field_test_taxonomy_term->entity = $term; we can't do that at all. But we can set default value of field just fine see http://cgit.drupalcode.org/dynamic_entity_reference/tree/src/Tests/Dynam...

Status: Needs review » Needs work

The last submitted patch, 15: der-2366093-15.patch, failed testing.

larowlan’s picture

larowlan’s picture

Status: Needs work » Needs review
StatusFileSize
new9.06 KB
new9.19 KB

Hate to say this - but I think I'm starting to understand typed data.
Don't tell anyone.

jibran’s picture

StatusFileSize
new8.55 KB
new11.85 KB

Thank you very much @larowlan for fixing it :)
Done some clean up and added another test. Haven't clean up DynamicEntityReferenceItem we'll do it in #2365331: Update DynamicEntityReferenceItem according to EntityReferenceItem.

jibran’s picture

Other then these issue I think it is RTBC.

  1. +++ b/src/Plugin/DataType/DynamicEntityReference.php
    @@ -63,36 +47,19 @@ class DynamicEntityReference extends EntityReference {
    +    elseif ($value instanceof EntityInterface) {
    

    I think DER should only support this case and throw exception for non entity items. ER has target_type in field_storage so it is ok for it to pass id as entity but not for DER. Thoughts??

  2. +++ b/src/Tests/DynamicEntityReferenceItemTest.php
    @@ -0,0 +1,205 @@
    +   * Tests the entity reference field type for referencing content entities.
    

    ops copy paste gone wrong :/

larowlan’s picture

We throw an error if an id is passed but no target_type is set on the parent
I can live with that.

jibran’s picture

Status: Needs review » Fixed
StatusFileSize
new906 bytes

I committed it with following doc fixes.

  • jibran committed 510b4ad on 8.x-1.x
    Issue #2366093 by jibran, larowlan: Unable to set field value...
jibran’s picture

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.