Reverse entity relationship I have several custom content entity types that have relationships to each another. The fields used are defined as base fields and I am using $fields['FIELD_NAME'] = BaseFieldDefinition::create('entity_reference').

Views doesn't currently implement reverse relationships for base fields, which is a feature request.

I need to define the relationship in the module with EntityViewData::getViewsData()  or hook_views_data().

Using the examples provided, I was able to define a reverse relationship for an entity reference field with cardinality of 1 (where the field is created in the entity's base_table), but I can't figure out how to do it when the cardinality is higher than 1, or unlimited (where the field is created in a separate table).

None of the examples on the documentation pages (or that I have been able to find googling)  defines a relationship between two entities that requires an intermediary table.  

Views defines an EntityReverse class that I have looked at for ideas.  It has a $first_join and a $second_join.  I can confirm that if the field is added to the entity using the normal UI, then the view provides both relationships, to the related entity and the reverse relationship from the related entity.

I am not sure at all how to translate the EntityReverse class into a definition I could pass to getViewsData().

And How to do a Reverse Entity Reference? is not the answer.

Does anyone know a solution?
After some trial and error, I have been able to find a solution.

In the example code for hook_views_data(), there was a relationship example that provided this snippet.

// ID of relationship handler plugin to use.
'id' =&gt; 'standard',


Having already discovered the EntityReverse class, I was pretty sure that I would need to use 'id' =&gt; 'entity_reverse'.

The part that tripped me up the most was that I was trying to define the relationship as part of the related entity inside getViewsData() in the ViewsData class I created for the related entity.  

Then it dawned on me that the documentation for hook_views_data() says:


  The return value of the getViewsData() method on the interface is the same as this hook


It means I can describe views data for any table and not just my entity's tables. Instead of trying to attach this relationship to the related entity, I tried attaching it to the main entity and this worked.

For example,


{MAIN ENTITY} is the entity being related
{RELATED ENTITY} is the entity with a relationship to {MAIN ENTITY} where {RELATED ENTITY} class has the following fields 

   $fields['{FIELD NAME}'] = BaseFieldDefinition::create('entity_reference')
     -&gt;setSetting('target_type', '{MAIN ENTITY}')
     -&gt;setCardinality(FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED) // or a specific number higher than 1



The getViewsData() method of the ViewsData class for {RELATED ENTITY} is the following.

public function getViewsData() {
  $data = parent::getViewsData();
  $data['{MAIN ENTITY BASE TABLE}']['{useful non-conflicting name - I used the {RELATED ENTITY BASE TABLE}}'] = [
    'title' =&gt; t('{some useful title that displays in Add Relationships popup}'),
    'help' =&gt; t('{some useful description that displays in Add Relationships popup}'),
    'relationship' =&gt; [
      'field_name' =&gt; '{name of the field - same as {FIELD_NAME}}',
      'field table' =&gt; '{name of intermediary table - it is probably {RELATED ENTITY}__{FIELD NAME}',
      'field field' =&gt; '{name of intermediary field - it is probably {FIELD NAME}_target_id',
      'base' =&gt; '{RELATED ENTITY BASE TABLE}',
      'base field' =&gt; '{RELATED ENTITY ID FIELD}',
      'id' =&gt; 'entity_reverse',
      'label' =&gt; '{some useful label that displays in Relationship select in various views field settings popups}',
    ],
  ];

  return $data;
}