How to Use Relationships in Drupal 8
With the Views module, it is possible to fetch pieces of data from a Drupal entity and display them according to a specific format. The Views module acts as a query builder, which generates the SQL code, in charge of retrieving the data from the database.
The first setting when creating a view allows you to choose the base table from which the aforementioned data will be recovered.
When you choose Content as the base table, you also have to specify the Content type, so it will not be possible to retrieve data from other content types and present it within the view unless you set a Relationship between those content types.
This tutorial will explain the concept of Relationships in Views with a basic example.
Let’s start!
Step #1. The Content Types
For this exampĺe, the content types we will be using are:
- Author
- Book
The fields for each one of them are detailed in the tables below:
Content type: Author |
|||
Field | Field type | Required | Allowed values |
Author image | Image | yes | 1 |
Date of Birth | Date | yes | 1 |
Date of Death | Date | no | 1 |
Body | Text (formatted, long, with summary) | no | 1 |
Content type: Book |
|||
Field | Field type | Required | Allowed values |
Cover image | Image | yes | 1 |
Year of publication | Number (integer) | yes | 1 |
Author | Content (Reference) | yes | Unlimited |
Body | Text (formatted, long, with summary) | no | 1 |
Notice that the Author field in the Book Content type references the Author Content type. This is how Drupal knows that these nodes are related. It is set to receive unlimited values since a book can have one or many authors.
After creating the content types, create some dummy content. Make sure that you create the Authors first, and then the Books. That way, you will always select an existing node to reference to.
Step #2. – Create the View
- Click Structure > Views > Add view
- Name the view Book listing
- Show Content of type Book
- Choose to Create a page
- Select Table as the Display format
- Uncheck Use a pager
- Click Save and Edit
- Add the following fields in the FIELDS section:
- Cover image
- Author image
- Body
- Click Add and configure fields
- Set the Image style to Thumbnail on both image fields
- Link the Image to the Content on both image fields
- Set the Body Formatter to Trimmed with a limit of 200
- Click Apply three times
- Rearrange the fields in the following order:
- Title
- Cover image
- Body
- Author image
If you scroll down to see the preview, you will notice that the Author’s image is not displaying. That’s because it belongs to another content type. We need to set a Relationship to the image field on the Author Content type.
Step #3. – Add a Relationship
- Click the ADVANCED link on the right of the Interface.
- Click Add in the RELATIONSHIPS option.
Feel free to take a look at all the fields from other tables to which is possible to relate the view to.
- Select Content referenced from field_book_author
- Click Add and configure relationships
- Check Require this relationship
- Click Apply
- Click Content: Author image (Author image) in the FIELDS section
You will see a new dropdown option on top called Relationship.
- Select (field_book_author: Content)
- Click Apply
- Save the view and take a look at the created page.
Relationships in Drupal help the site-builder to create complex views, by using data from different tables and presenting them in a logical manner.
I hope you liked this tutorial. Thanks for reading!
Thanks. This is going to be very helpfull for me.
Love your articles on Drupal 8. Keep it up!!
Glad to hear that!
Regards.
There’s so little material online that talks about Relationships in Views that this is a very welcome resource. Thanks for taking the time to write it up and share it.
Hi JLH,
glad to help!
Thanks, a good example to showcase relationships between content types
THnks, i just need some more help, I have two content types and both are linked with ref field. Suppose a content type for Provider and Product. In a view i create a relationship through views relationship section. If i chose a content type Provider as a filter, and then make relationship with Product content type, so in a table view it is showing only Provider those have Product , but do not display Provider those do not have Product, i want to put some test like “There is not product available for this Provider” against each provider who does not have Product, so how it is possible? Thanks a lot in advance.
How will you do if the field to be displayed comes from 3rd related table