Allow more field types to be used as reference filter fields

Created on 7 September 2018, over 6 years ago
Updated 16 October 2023, about 1 year ago

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.
Feature request
Status

Needs work

Version

3.0

Component

Code

Created by

🇫🇮Finland mikran

Live updates comments and jobs are added and updated live.
  • Needs tests

    The change is currently missing an automated test that fails when run with the original code, and succeeds when the bug has been fixed.

Sign in to follow issues

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