Views: relationships

17/02/2022
Drupal Header image

Views are one of the most powerful components of Drupal and a very useful tool for the sitebuilding.

We can create pages, blocks, show lists, filter contents, choose formats, etc. without using any programming, through its user interface.

In this article we are going to talk about one of the options found in the "Advanced" section when we configure a view, which will allow us to create relationships between different entities on our website.

For example, we have a Document content type, with a link to a file, and we want to display the name and surname of the author of the document in a list.

Views: relationships - Image 1

 

To do this, we must relate the fields "Name" and "Surname" of the user profile with the document node created by the author.

When we create the view of the Document content type, we see that we do not have a user's fields available in the list of available fields.

Views: relationships - Image 2

 

To have these fields available, we must go into the "Advanced" section and in "Relationships", click on "Add" and select "Content author".

Views: relationships - Image 3Views: relationships - Image 4

 

By adding the relationship with the User entity, we will now have all the User fields available, although the view is of the Document content type. When adding fields, we see that the "User" Category is now available.

Views: relationships - Image 5

 

These User fields will also be available for the others configurations of the view (filters, sort criteria, replacement patterns, etc.). In our case we want to add the fields "Name" and "Surname".

Views: relationships - Image 6

 

The view would be configured as follows:

Views: relationships - Image 7

 

And the result would be:

Views: relationships - Image 8