Views relationships don't work on reference field

Created on 6 August 2025, 10 days ago

Problem/Motivation

Views relationships don't appear

Steps to reproduce

1. I have ECK custom entity https://www.drupal.org/project/eck β†’
2. Two bundles A and B
3. Bundle A has datafield, with column of 'entity reference' type referencing bundle B.
4. I would like to filter bundle A by bundle B value, so I would like to add "relationship", but there are no datafield relationships to select.

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

πŸ› Bug report
Status

Active

Version

2.0

Component

Code

Created by

πŸ‡±πŸ‡ΉLithuania mindaugasd

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

Comments & Activities

  • Issue created by @mindaugasd
  • πŸ‡±πŸ‡ΉLithuania mindaugasd

    What I found so far:

    datafield.views.inc

    has these lines at the top:

      $field_name = $field_storage->getName();
      $columns = $field_storage->getSetting('columns');
      $field_settings = $field_storage->getSetting('field_settings');
    

    $columns is filled, but $field_settings is empty.

  • πŸ‡±πŸ‡ΉLithuania mindaugasd

    Fixed one bug for now, but don't work yet.

    Replaced

    $field_settings = $field_storage->getSetting('field_settings');
    

    with

      $field_configs = \Drupal::entityTypeManager()
        ->getStorage('field_config')
        ->loadByProperties([
          'field_name'  => $field_name,
          'entity_type' => $field_storage->getTargetEntityTypeId(),
        ]);
      $field_config = reset($field_configs);
      $field_settings = $field_config ? $field_config->getSetting('field_settings') : [];
    

    Based on the type of bug, it means this feature was not tested yet.

  • πŸ‡±πŸ‡ΉLithuania mindaugasd

    The rest of code AI fixed. I don't know what it did, but it rewrote a lot of code and now it works. I attached the patch for version 2.0.11.

    This is what it said:

    β€’ Correct variable $target_entity_type_id is used instead of the wrong $field_settings['target_type'].
    β€’ Added failsafes so the hook does nothing if no instance exists or if the referenced entity type is missing.

    // TODO:
    β€’ If you have the same field storage reused by several bundles and the sub-field β€œentity_reference_type” differs between instances, you might want to iterate over all $field_configs and create one relationship per encountered target entity type.

  • πŸ‡±πŸ‡ΉLithuania mindaugasd

    Updated patch to fix notice:

    Notice: Only variables should be passed by reference in datafield_field_views_data() (line 61

    • 7d91dc09 committed on 2.x
      Issue #3540021: Views relationships dont work on reference field
      
  • πŸ‡«πŸ‡·France lazzyvn paris
Production build 0.71.5 2024