Problem/Motivation

When mapping to an entity reference field, Feeds allows to you to lookup existing entities using several fields on that entity:

The list of field types that can be used is limited. It would be nice if a wider range of field types can be used to reference by, like the email field type for example.

This is the current implementation of EntityReference targets filterFieldTypes method.

protected function filterFieldTypes(FieldStorageDefinitionInterface $field) {
  if ($field instanceof DataDefinitionInterface && $field->isComputed()) {
    return FALSE;
  }

  switch ($field->getType()) {
    case 'integer':
    case 'string':
    case 'text_long':
    case 'path':
    case 'uuid':
    case 'feeds_item':
      return TRUE;

    default:
      return FALSE;
  }
}

So it is whitelist. File target class uses different black list approach where is marks filesize and filemime as non-sensible fields and all others are OK to use.

Would it be better to alter this EntityReference behaviour to match that of File? What field types are not suited as filter fields?

Core field types are:

  • boolean
  • changed
  • comment
  • created
  • datetime
  • daterange
  • decimal
  • email
  • entity_reference
  • file
  • float
  • image
  • integer
  • language
  • link
  • list_float
  • list_integer
  • list_string
  • map
  • password
  • path
  • string
  • string_long
  • telephone
  • text
  • text_long
  • text_with_summary
  • timestamp
  • uri
  • uuid

Proposed resolution

Expand the list with field types that can be used as something to reference by. Add test coverage for each of them to make sure they can in fact be used as field to reference by.

Steps for adding test coverage, for each field:

  1. Check if the existing CSV file at tests/resources/csv/content.csv has a data column that could be used for the field type that we're testing with. If not, add a column to this CSV file with test data.
  2. Add a test method to \Drupal\Tests\feeds\Kernel\Feeds\Target\EntityReferenceTest.
  3. In the test:
    1. Create a target content type called 'foo'.
    2. Add a field of the type to test to the content type 'foo'. For example the field type 'decimal'.
    3. Add an entity reference field to the content type 'article', allow it to reference nodes of type 'foo'.
    4. Add a feed type that maps to guid, title and the entity reference field.
    5. Create at least one node of type 'foo'. Set a value for the field to test, for example for the 'decimal' field.
    6. Create a feed and import the content.csv test file.
    7. Assert on the imported nodes that a node of type 'foo' is correctly referenced.

Remaining tasks

  1. For each of the specified core field type, add a kernel test to ensure they can be used as a field to reference by:
    • decimal
    • email
    • feeds_item
    • float
    • integer
    • link
    • path
    • serial
    • string
    • string_long
    • telephone
    • text
    • text_long
    • text_with_summary
    • uri
    • uuid
    • year

    Some of the above may not be suitable. You can remove these from the list above.

  2. Check if you can find more field types that are candidate for being suitable as something to reference by.
  3. Adjust the existing patch: remove field types from the list that were not suitable and add ones you found suitable.

Comments

mikran created an issue. See original summary.

mikran’s picture

Assigned: Unassigned » mikran

From the list of core fields I think it makes sense to add at least these:

  • decimal
  • email
  • float
  • link
  • string_long
  • telephone
  • text
  • text_with_summary
  • uri
mikran’s picture

Status: Active » Needs review
StatusFileSize
new955 bytes

and a patch that updates the list of suitable fields.

megachriz’s picture

Status: Needs review » Needs work
Issue tags: +Needs tests

Good suggestion.

I do think these would need tests to ensure each supported field type to reference by works. For example, referencing entities by fields of type 'link' or 'text_with_summary' did not work in my case. For the link field I tried to reference an existing entity by providing an URL in the source file. For the text with summary field I tried to reference an existing entity by using text typed in 'value' part. Using the 'summary' part did not work either.

I also tried 'email' and 'telephone' and these did work well in my manual testing.

megachriz’s picture

Issue tags: +beta target

Adding this as a nice to have before beta phase.

govind.maloo’s picture

StatusFileSize
new351.46 KB

@megachriz- Added fields types doesn't fit with mapping properly. I can see fields in mapping bu tonn import it is not working. Float decimal email is oka ut not other fields type is working.

We can move this further adding those only.

superlolo95’s picture

Dears,
I would need some support.
I created a custom target plugin to support Year Only (year) and Serial (serial) contrib fields.
Before I upgrade to the last (stable) version of feeds, it seemed to work to use those fields as reference filter fields.
As a workaroud, I patched the /drupal/modules/contrib/feeds/src/Feeds/Target/EntityReference.php as below to add year and serial as reference filter fields.
What would be the clean way to achieve that?

  protected function filterFieldTypes(FieldStorageDefinitionInterface $field) {
    if ($field instanceof DataDefinitionInterface && $field->isComputed()) {
      return FALSE;
    }

    switch ($field->getType()) {
      case 'integer':
      case 'string':
      case 'text_long':
      case 'path':
      case 'uuid':
      case 'feeds_item':
      case 'year':
      case 'serial':
        return TRUE;

      default:
        return FALSE;
    }
  }
megachriz’s picture

Issue summary: View changes
Issue tags: +BADCamp2022

Tagging for BADCamp and updated issue summary. This one mostly needs tests. It can be a lot of work to handle that in one day though. So the person picking this up during the sprint could decide to work on test coverage for just one field type as a first step.

megachriz’s picture

Support for referencing existing entities by email is added in #3207793: Allow import of entity reference fields by email.

superlolo95’s picture

Hello,
Would it be possible to add the support of custom Feeds Target plugins ?