I'm building views on custom Entities. One of my Entities has a field which is an entity_reference with unlimited cardinality:

$fields['subjects'] = BaseFieldDefinition::create('entity_reference')
                        ->setLabel(t('Subjects'))
                        ->setSetting('target_type', 'pw_subject')
                        ->setCardinality(BaseFieldDefinition::CARDINALITY_UNLIMITED)
                        ->setdisplayOptions('view', array(
                          'type' => 'entity_reference_entity_label'))
                        ->setDisplayOptions('form', array(
                          'type' => 'entity_reference_autocomplete'));

Entity API creates a join table:

+--------------------+------------------+------+-----+---------+-------+
| Field              | Type             | Null | Key | Default | Extra |
+--------------------+------------------+------+-----+---------+-------+
| bundle             | varchar(128)     | NO   | MUL |         |       |
| deleted            | tinyint(4)       | NO   | PRI | 0       |       |
| entity_id          | int(10) unsigned | NO   | PRI | NULL    |       |
| revision_id        | int(10) unsigned | NO   | MUL | NULL    |       |
| langcode           | varchar(32)      | NO   | PRI |         |       |
| delta              | int(10) unsigned | NO   | PRI | NULL    |       |
| subjects_target_id | int(10) unsigned | NO   | MUL | NULL    |       |
+--------------------+------------------+------+-----+---------+-------+

When I try to add a views relationship to subjects views adds a left join to subjects using the non-existant subjects column rather than subjects_target_id

LEFT JOIN {pw_subject} pw_subject_pw_contact__subjects ON pw_contact__subjects.subjects = pw_subject_pw_contact__subjects.id

[I have also asked about this on stackexchange]

Comments

skitten created an issue. See original summary.

skitten’s picture

After a lot of time with XDebug I think the issue is in mapFieldDefinition() (EntityViewsData.php) - if I set $views_field_name = $schema_field_name (rather than $field_name) I get a query that works.

I'd love if someone who understands the views (and schema) code could tell me if this makes sense...

skitten’s picture

Version: 8.1.8 » 8.2.x-dev
gabesullice’s picture

Status: Active » Needs review
StatusFileSize
new974 bytes

I have replicated this issue (by having it myself ;) ) and can confirm that the suggestion in #2 resolves the problem.

Status: Needs review » Needs work

The last submitted patch, 4: views_can_t_make_a-2795455-4.patch, failed testing.

gabesullice’s picture

Title: Views can't make a relationship to an entity field with cardinality > 1 » Views does not add relationship data for entity_reference fields not defined in configuration
Status: Needs work » Active

After a lot more digging, I think the previous patch is simply a coincidental fix (obviously since it failed). I think the root cause is that the views module is only adding 'relationship field' data for entity_reference config fields, not base fields on entities.

If you look in views.views.inc, you'll notice an implementation of hook_field_views_data, which adds this relationship data for all entity reference fields. The issue is that it is only called for fields which are defined in config, i.e., the code is not called for any entity_reference fields added on any custom entity types or added using hook_entity_base_field_info.

dawehner’s picture

Issue tags: +Needs tests

Yeah, this gap between the way how we generate configurable fields and base fields is just sad.

+++ b/core/modules/views/src/EntityViewsData.php
@@ -374,7 +374,7 @@ protected function mapFieldDefinition($table, $field_name, FieldDefinitionInterf
     foreach ($field_column_mapping as $field_column_name => $schema_field_name) {
-      $views_field_name = ($multiple) ? $field_name . '__' . $field_column_name : $field_name;
+      $views_field_name = ($multiple) ? $field_name . '__' . $field_column_name : $schema_field_name;
       $table_data[$views_field_name] = $this->mapSingleFieldViewsData($table, $field_name, $field_definition_type, $field_column_name, $field_schema['columns'][$field_column_name]['type'], $first, $field_definition);

I could totally imagine that this is the right fix, but yeah we need some test coverage to ensure it happens.

gabesullice’s picture

StatusFileSize
new960 bytes

Okay, I think I've found a more appropriate solution.

gabesullice’s picture

Status: Active » Needs review

Kicking tests.

lendude’s picture

Status: Needs review » Needs work
+++ b/core/modules/views/src/EntityViewsData.php
@@ -581,6 +581,15 @@ protected function processViewsDataForEntityReference($table, FieldDefinitionInt
+      if ($field_definition->getFieldStorageDefinition()->isMultiple()) {
+        $database_field = $field_definition->getName() . '_target_id';

Maybe use $field_definition->getFieldStorageDefinition()->getMainPropertyName() instead of hardcoding target_id?

Setting back to needs work for the test coverage.

geekygnr’s picture

#8 solves the problem for me.

skitten’s picture

Yeah, I also came to the conclusion of #6 that configurable fields do it right and base fields don't. So in the end I just had to add this field afterwards as a configurable field. Thanks for investigating!

tahirmus’s picture

Status: Needs work » Needs review
StatusFileSize
new1023 bytes

Added $field_definition->getFieldStorageDefinition()->getMainPropertyName() as suggested in #10

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.6 was released on February 1, 2017 and is the final full bugfix release for the Drupal 8.2.x series. Drupal 8.2.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.3.0 on April 5, 2017. (Drupal 8.3.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.3.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Roensby’s picture

Patch in #13 works for me. Thank you everyone for the work!

My test case:
Three entity types, A, B and C.
Entity type A has the following fields:
- entity reference field for B with a cardinality of 1
- entity reference field for C with unlimited cardinality

Create a view for A.
Add relationship to B.
Add a field with relationship to B.
This works both before and after patch.

Add relationship to C.
Add a field with relationship to C.
This fails before patch and works after patch.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.6 was released on August 2, 2017 and is the final full bugfix release for the Drupal 8.3.x series. Drupal 8.3.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.4.0 on October 4, 2017. (Drupal 8.4.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.4.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

sdrong’s picture

Confirming Patch #13 addresses CARDINALITY_UNLIMITED reference id issue when custom entity is defined in the class vs type/bundle db configuration. Please add to core or document preferred method of defining custom BaseFieldDefinitions for view compatibility.

gabesullice’s picture

Status: Needs review » Reviewed & tested by the community

Marking as RTBC by @sdrong

tacituseu’s picture

Status: Reviewed & tested by the community » Needs work

Per #10 it still needs test coverage,

benjy’s picture

The patch doesn't align with the issue title here? It's not only multi cardinality fields that are an issue, any ER field that isn't defined in configuration is ignored by core_field_views_data() #2895578: Views does not add reverse relationship data for entity_reference fields not defined in configuration could be a duplicate or we could rename the issue title here?

borisson_’s picture

I agree with @benjy, this patch doesn't do enough to fix the problem for single-value entity reference base fields. I think it's best to re title this issue and keep #2895578: Views does not add reverse relationship data for entity_reference fields not defined in configuration to do the other work.

We do have a views data unit test, but this looks like something that we would want to test with at least a kernel test. Is there a kernel test we could use to do this with or is a functional test sufficient?

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.4 was released on January 3, 2018 and is the final full bugfix release for the Drupal 8.4.x series. Drupal 8.4.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.5.0 on March 7, 2018. (Drupal 8.5.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.5.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

jefuri’s picture

+++ b/core/modules/views/src/EntityViewsData.php
@@ -581,6 +581,15 @@ protected function processViewsDataForEntityReference($table, FieldDefinitionInt
+        $views_field['filter']['field'] = $database_field;

Missing an option for $views_field['filter']['argument'] here. This will still throw an SQL error when using contextual filters.

jefuri’s picture

Title: Views does not add relationship data for entity_reference fields not defined in configuration » Views does not add relationship data for entity_reference fields not defined in configuration and are multiple
Version: 8.5.x-dev » 8.6.x-dev
Status: Needs work » Needs review
StatusFileSize
new1.07 KB

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

acbramley’s picture

This issue also applies to other non entity reference fields as well. For example I have an custom entity type with a file field. The views_views_data() function invokes hook_field_views_data but only for fields defined by a field_storage_config entity, so file_field_views_data() isn't called for my custom entity's file field, therefore the relationship isn't added.

joachim’s picture

I’m not quite sure what you mean by ‘non entity reference fields’ — isn’t a file field a type of entity reference field?

acbramley’s picture

@joachim I guess you could argue that it is, but they're their own field type which is used as a prefix for hook_field_views_data() (e.g file_field_views_data()).

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

nicxvan’s picture

I believe I am encountering this issue.

The patch applies to 8.7.5 but does NOT fix the issue.

In my case I have a view that uses contextual filters to grab the user id.
It then creates a relationship to a entity reference field on that user for organizations and from that org entity it grabs a list of products referenced on the org entity.

The preview shows the proper list of products for a given user.

When I create an entity reference field that uses the view to populate itself the list is just organizations not the products.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

bojanz’s picture

Status: Needs review » Closed (outdated)

The problem from the issue description was fixed in #2846614: Incorrect field name is used in views integration for multi-value base fields / Drupal 8.5.

#28 is about #2337515: Allow @FieldType to customize views data.
#32 sounds unrelated.