After debugging, I found the following issue: when we use an entity_reference field (with an entity browser for selection), we are not able to save default values properly:

$field_value, in our case looks like [ 'target_id' => 'media:123' ] where "123" is the mid of the media item. This is not valid and throws inside "PrimitiveTypeConstraintValidator", since this is supposed to be an integer, but 'media:123' is a string, but '123' would be valid.

  if (isset($field_value['target_id']) && is_array($field_value['target_id'])) {
        $field_value = $field_value['target_id'];
      }

I think this should handle this case, but $field_value['target_id'] is not an array and would still contain 'media:123'

Is there a special use-case, we are missing to handle references?

Comments

DrColossos created an issue. See original summary.

drcolossos’s picture

Just a quick heads-up:

If I add

if (isset($field_value['target_id']) ) {
   [$type, $field_value] = explode(':',$field_value['target_id'] );
}

after your "odd term structure" fix, everything works as expected for the reference.

pratik_kamble’s picture

StatusFileSize
new1.27 KB

Patch to fix the issue related to the media reference entities.

b_sharpe’s picture

Status: Active » Needs review
StatusFileSize
new1.23 KB
new956 bytes

This was causing me undefined index errors when the handler was a single item (i.e. views, layout_library), also when media was used but the handler was not default:media the structure was different.

I've updated the patch to put in these checks, if someone wants to validate I will put this into next release.

ricardochefigueroa’s picture

#3 This patch worked for me. Thanks.

Lal_’s picture

Moving the issues to RTBC as the patch solves the issues.... Adding the screen shots.

Lal_’s picture

Status: Needs review » Reviewed & tested by the community

  • b_sharpe committed 10fdc6f on 8.x-1.x
    Issue #3086275 by b_sharpe, pratik_kamble, Lal_: Usage with Entity...
b_sharpe’s picture

Status: Reviewed & tested by the community » Fixed

Thanks all, committed!

Status: Fixed » Closed (fixed)

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

scotwith1t’s picture

For me, this still only works with the autocomplete widget and does not set the default value on existing content when Media Library is set as the widget...I had to set to use autocomplete, resave to get the batch to run again, then switch back to Media Library for the correct/desired UX.

b_sharpe’s picture

Widget submission format is definitely an issue, unfortunately I'm not sure how to remedy short of creating a case for each widget individually. For core that's likely ok and minimal, but contrib widgets will likely never be supported.