Problem/Motivation
core_field_views_data() adds both forward and reverse relationship data for entity_reference fields defined in configuration. Same should be done for entity_reference fields NOT defined in configuration, like entity_reference baseFields.
Proposed resolution
Forward relationships for entity_reference baseFields are added in \Drupal\views\EntityViewsData::processViewsDataForEntityReference(), but reverse references cannot be added here as they have to be on different tables than this method can handle. For the same reason, \Drupal\views\EntityViewsData::mapFieldDefinition() is not applicable either. As the cycle at the end of views_views_data() triggers core_field_views_data() but only for fields stored in configuration, I think it would be a good place to start checking to add reverse relationships for entity_reference baseFields (possibly some other extra corner cases might be handled here which I'm unaware of yet).
Comments
Comment #2
boobaaAttaching some possibly related issues.
Comment #3
boobaaComment #4
boobaaI'm working on a module which provides several entity types with entity reference baseFields between them, both single- and multi-value ones. Here's how I got around this problem.
I know it's somewhat quick and dirty, but hopefully helps moving forward.
Comment #7
borisson_As soon as we get a test for #2795455: Views does not add relationship data for entity_reference fields not defined in configuration and are multiple, writing a test for this issue should be trivial, but at the very least I'm tagging this as needs tests.
The code writting in #4 can be used as a starting point, and I think this part is the one that we'd need to use:
However, that is just from looking at the code here, I need this for a project (with only single-value er-basefields), so I will report back when I get this working.
Comment #8
borisson_In the code I quoted earlier, there needed to be one change:
$field_table = $target_entity_type->getBaseTable();Comment #9
borisson_@berdir mentioned
\Drupal\Tests\views\Unit\EntityViewsDataTestas a possible place to test this. Let's see if that will work.Comment #10
tacituseu commentedDuplicate of #2706431: provide Views reverse relationships automatically for entity base fields ?
Comment #12
majdi commentedduplicate issue see #2706431: provide Views reverse relationships automatically for entity base fields