Entity lookup can't find an entity when looking for a reference field

Created on 8 August 2021, over 3 years ago
Updated 13 April 2023, over 1 year ago

Problem/Motivation

If you want to search for an entity based on one of its referenced file (entity reference, file...) entity lookup won't be able to find it.

Steps to reproduce

For example when migrating an old content type which was using file field to a new CT using media, you will first create the media and then when creating the destination content you will look for the old file ID to link it to the newly created media.

Here is an example :

process:
  field_media_file:
    -
      # find corresponding fid of migrated file
      plugin: migration_lookup
      source: field_old_file
      migration: medias_files_migration
    -
      # find which media has been created using this fid
      plugin: entity_lookup
      entity_type: media
      bundle_key: bundle
      bundle: document
      value_key: field_media_document

This code won't be able to find the correct media.

Proposed resolution

This bug seems to come from EntityLookup:query() and especially this part :

$result_value = $entity instanceof ConfigEntityInterface ? $entity->get($this->lookupValueKey) : $entity->get($this->lookupValueKey)->value;

This last part use ->value which will work for most fields but not for entity reference as they're using target_id.

🐛 Bug report
Status

Needs work

Version

6.0

Component

Plugins

Created by

🇫🇷France tostinni

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

Merge Requests

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024