Introduction
For this article we used:
- Drupal 9.5.7
- Devel 5.1.1 And its Devel Generate submodule to generate dummy content for testing.
We wanted to let our course designers indicate which other articles they consider related content. Having related content readily available to our learners lets them discover content their learning path might otherwise not have led them to.
In this article we'll demonstrate how to relate content together via content reference fields, and how to create a block of related articles, to be shown on article pages.
In later articles we'll highlight other approaches to the concept of related content.
About reference fields
Reference fields in Drupal let you link content entities (such as nodes, users, terms, and blocks) to each other. Think of them as pointers to other items that you consider to be related.
Example scenarios include:
- A Book content type with an "Authors" content reference field to select one or more Author nodes
- An Article content type with a "Categories" term reference field to select one or more taxonomy terms
- A user account with a "Referred by" user reference field to select the user who referred them.
All reference fields are entity reference fields. A content reference field is an entity reference field that lets you reference content (nodes), a user reference field is an entity reference field that lets you add users, and so on.
The following diagram illustrates a content reference field (field_related_reports) on a node of the type report. The Annual Report 2023 references two older reports.
Step 1 - Add content reference field
1.1 Add field
Add a field of the type content reference to your Article content type:
- Name: field_related_articles
- Reference method: default
- Content type: Article
- Allowed number of values: 5 (optional)
These settings limit the type of content you can reference to Article. In other words, you're adding a field to the Article content type from where you can only reference (five) other Article nodes.
1.2 Manage display
In the Article content type's Manage display settings:
- disable the field you just added (you don't want to display the related article list below the content, but rather in the block you'll build later on.)
1.3 Manage form display
In the Manage form display settings:
- Widget: Check boxes/ Radio buttons
Note
Select lists and checkboxes work well for a small number of options; for a large number of options you'll want an autocomplete field or alternative (third-party) form widgets such as Select2 or Chosen.
Step 2 - Create views block
- Title: Suggested reading
- Show: Content of the type Article
- Format: show Fields
2.1 Fields
- Title (content)
2.2 Filter criteria
- Content type (content) = Article
- Published: yes
2.3 Sort criteria
- Authored on (content): desc
2.4 Contextual filters
- Name: ID (content)
- When the filter is not available: Provide default value: Content ID from URL
This block will be shown on Article pages; the contextual filter ensures you'll only retrieve articles related to (referenced by) the current article.
At this point your block view is configured to retrieve information for 1 single article: the article alongside which the block is shown. In the next two steps you'll pull in the referenced articles.
2.5 Relationships
- Title: Content referenced from field_related_articles
This relationship lets you pull in the related articles.
2.6 Fields (again)
- Title
- Relationship: field_related_articles
In this last step, by using the newly defined relationship on the Title field, you're telling Drupal to show the related articles' titles, not the current article's title.
2.7 View definition summary
- You created a block view of articles, using a contextual filter to only load the article on whose page the block is shown.
- You then added a relationship to fetch the related articles.
- Lastly, you used the relationship to show each related article's title.
And that's it. The block view should now show content related to the main article alongside which the block is displayed.
Conclusion
You don't need additional modules to build a concept of "related content".
In this scenario content editors have absolute control and can specify related content manually for each article. However, this flexibility comes with a cost: related content needs to be set and maintained for each article individually.
Other approaches to "related content", which will write about in follow-up articles, include:
- content related based on shared taxonomy term
- content related based on shared author
Summary
- Add a (multi-value) content reference field to your content type.
- Stop the field from being displayed by default.
- Update existing articles and specify related articles.
- Create a block display that lists related content for a given article.
- Place the block alongside articles.