Skip to main content
Logo

Show related content with relationships in Drupal 8 Views (+video)

Blog image

Your website can be a mirror of your business ideas reflecting them exactly. Displaying data the way you want it to makes your website more user-friendly. Today, we discuss how relationships in Drupal 8 Views can be helpful if you want to show related content. The video will make our story more vivid.

By showing related content we mean including the content somehow related to the main one into the page display. It’s great when you can show, for example, the favorite articles of a user, the reviews and the blog posts about a product together with their author pictures, and so on. The unquestionable benefits of showing related content are:

  • the pages look more interesting
  • this creates a richer content ecosystem
  • users are kept engaged and stay on the site
  • your website’s session duration increases
  • the chance for conversion grows

Luckily, there are great tools for this. Relationships in Drupal 8 Views allow you to show related content in very flexible ways with no limits to your ideas. Let’s get acquainted with them step by step.

What are Views in Drupal 8?

Whatever Drupal website you see, many of its pages are likely to be built with Views. Drupal Views is the SQL query builder that fetches data from your website’s database (content, users, comments, etc.) and displays them on your site as a data collection.

Example of Drupal Views on a website

This does not require coding — Views has a user interface to configure which data you want to display and how. You can sort and filter the results, choose to show dynamic data via contextual filters in Drupal 8 Views, and so on.

The Views are built into Drupal and does not require installation, which is among the best Drupal 8 benefits. However, dealing with Drupal Views requires a profound understanding of Drupal and should be entrusted to a web development team.

What are relationships in Drupal 8 Views?

When created, every Drupal view is configured to retrieve the data from a particular database table.

Choosing base table in Drupal 8 Views

To create richer displays and show related content, relationships in Drupal 8 Views connect your initially chosen table to others. So, thanks to relationships added, the view becomes able to display the data from different tables together.

Here are a few scenarios when relationships in Drupal Views can help you show related content. For example, you can:

  • show related author pictures in articles
  • show related user pictures in comments
  • show related favorite content in user profiles
  • show related real estate properties in owner profiles
  • show related user mentos’ pictures in user profiles

and much more.

How to use relationships in Drupal 8 Views?

Let’s create relationships in Drupal 8 Views that will help us display customer reviews together with their pictures. We will need to show the data from the “Content” table together with the related data from the “Users” table.

1. Creating the Review content type

Let’s go to Structure — Content types and create a new content type called “Review.”

Creating a Drupal 8 content type

2. Adding a user field

Our reviews will belong to particular customers who are users on the Drupal website. So we need to add a “user” field to the Review content type. It will be a referenced entity. We will name it “Review author.”

Adding a referenced entity user field to a Drupal 8 content typeGiving a name to a user field in a Drupal 8 content type

3. Adding test reviews

For testing purposes, we then add a few actual reviews in “Add content — Reviews”. We assign a different test user to each of them in the “Review author” field.

Adding Drupal content with the author field

4. Creating a view based on reviews

In Structure — Views we add a new view:

  • give it a name “What our customers say”
  • set it to display content of the Review type
  • choose “create a page”
  • set its display format to “Grid of fields”
Creating Drupal 8 Views

5. Adding fields to the view

Our view is based on fields, so let’s add the ones we need:

  • review author
  • body

The review title, the default field for a content type, will not be needed and can be removed from our view.

Adding fields to Drupal 8 Views

6. Adding a relationship to the view

We will also need an author picture, for which we go to the “Advanced” section in Views, click on “Relationships,” and add a new relationship. By searching through the available options, we choose “User referenced from field_review_author.”

Adding relationships to Drupal 8 Views

By clicking “Add and configure relationship,” we are almost done — we may only check “Require this relationship.”

Then we come back to the Views fields and add the “User Picture” field. When configuring the field, make sure the “field_review_author” relationship is selected. To make all pictures look standard, apply an image style.

Adding user picture field to Drupal 8 Views that has a relationship

Now the user picture will be fetched to our page from every review author profile to make it all look much more colorful now. This is our page with customer reviews created via a relationship in Drupal 8 Views! But, of course, it is a rough version that needs some good theming.

Drupal 8 Views with a relationship

Video about adding a relationship in Drupal 8 Views

We've started a good tradition of sharing the information from our blog posts in a video, so enjoy watching our example of how to use relationships in Drupal 8 Views.

Entrust our team with displaying your website’s data right

The above was a very simple example of how to use relationships in Drupal 8. There is really no limit to how your data can be displayed and your pages built. You can display related content in any way, or anything else you might have an idea about.

Share your ideas with our Drupal maintenance and support team so they will come up with a good solution and bring it to life. 

Read Also

  • Headless Drupal in 2024: React and GraphQL Architecture Overview
    Customer Success StoryDrupalOur TMS Guides

    Headless Drupal in 2024: React and GraphQL Architecture Overview

    Arrow icon
  • Why Drupal Is Your Startup's Secret Weapon 🚀
    DrupalOur TMS Guides

    Why Drupal Is Your Startup's Secret Weapon 🚀

    Arrow icon
  • Why Your Drupal Agency is Bleeding Money (And Your Competitors Already Know the Fix)
    Customer Success StoryDrupalOur TMS Guides

    Why Your Drupal Agency is Bleeding Money (And Your Competitors Already Know the Fix)

    Arrow icon

Don't miss the chance. Let's work together

Play around with first 100$. Try and add your wishes later.