Let's consider a views on an entity named :"Person" . "Person" has a field named "id" (the primary key) "PersonTag" has a field named "person_id" (this field is referencing the id of "PersonTag" entity.
My issue is that I want to display in this views the fields of "PersonTag". First idea was to try to implement a join with Person using hook_views_query_alter() . That was the main purpose of this question. Well now I'm having the join on my query, but can't figure out how to display the fields (of the join table) in my views.
So after some searches, I realised that I should use hook_views_data() as it explained in this question.
In documentation of hook_views_data() it's mentionned that:
To provide views data for an entity, instead of implementing this hook, create a class implementing \Drupal\views\EntityViewsDataInterface and reference this in the "views" annotation in the entity class.
Since I'm having this class already created, I tried to implement my join in there. Here are the code implemented:
class PersonViewsData extends EntityViewsData implements EntityViewsDataInterface {
/**
* {@inheritdoc}
*/
public function getViewsData() {
$data = parent::getViewsData();
$data['person']['table']['base'] = array(
'field' => 'id',
'title' => $this->t('Person'),
'help' => $this->t('The Person ID.'),
);
$data['person']['table']['join'] = array(
'person_tag' => array(
'left_field' => 'person_id',
'field' => 'id',
),
);
$data['person']['id'] = array(
'title' => t('Example '),
'help' => t('Example'),
'relationship' => array(
// Views name of the table to join to for the relationship.
'base' => 'person_tag_field_data',
// Database field name in the other table to join on.
'base field' => 'person_id',
// ID of relationship handler plugin to use.
'id' => 'standard',
// Default label for relationship in the UI.
'label' => t('Example '),
),
);
return $data;
}
}
This code doesn't work, I don't see any field appearing in my view.
Would help me to figure out the solution?