Related Articles Block with Taxonomy Terms in Drupal 8

28 Nov 2017
Related Articles
Youtube Video

This article is a step wise guide to demonstrate the creation of related articles block in Drupal 8. Related articles works on the basis of tags used in contents. So, tags with similar tags gets listed in related articles block. This helps user to go through the similar articles on your site and result in more satisfied users. We can create this with help of views in Drupal.

Create Related Articles Block - Steps:

Let's start with what I think is the most common scenario when creating a blog post. You create a blog post, tag it with all the relavent tags, and you want to show in the sidebar additional blog posts that have similar tags.

for this we'll start with a plain new installation of Drupal 8, which comes loaded by default with the "Article" content type, as well as the the "Tags" Vocabulary and field on Articles. Now, let's create some dummy articles with some tags.

Next, create a new view:

  1. Call it Related Articles
  2. Show: Content
  3. Type: Articles
  4. Create a page: Unchecked
  5. Create a block: Check
  6. Continue & Edit

Create View

Ok, the first thing we need to do is access information about out Tags taxonomy terms on the article. Since taxonomy terms are stored on the article as reference field between the article node entity and the taxonomy term entity, we need to add a relationship between the node and the taxonomy based on the Tags field.

Click on the "Advanced" link to show the...well advanced options.

Add a Relationship, and search for the taxonomy term reference field name - Tags. Check the "Content: Tags (field_tags)" option, and click the "Apply (all displays)" button.

Related Articles - Add Relation

Click "Apply (all displays) again.

Now we can do the smart stuff, basically this is what we want to happend: when you're viewing an article, you want to show other articles that have the same tags in common with the current article. 

Currently our view will show all the articles (click "Update preview" at the bottom), so we want to filter out all the irrelavent articles.

Add a Contextual Filter, filter to "Taxonomy Term", and no check "Taxonomy term: Term ID", we want to filter the results according to Taxonomy term ids.

...Apply...

Views expect contextual filters to be given via the url, on a page with the url "blog", you'd pass a contextual filter like so: "blog/1". But we don't want to modify the url, we're just viewing an article right?

Under "When the filter value is NOT Available" select "Provide default value"

Next, under "Type" select "Taxonomy term ID from URL" (we're on an article, and we want to get their terms/tags from that article).

Now, here comes the real magic: Check " Load default filter from node page, that's good for related taxonomy blocks" , and "Limit Items by vocabulary", and "Tags" (this is optional, if you have multiple taxonomies on an article you don't have to limit the results).

Multiple-Value handling -> "Filter to items that share any term" - do we want only articles that share all the tags, on is just one enough.

contextual filters

Click on the "More" link at the bottom.

Check "Allow multiple values", I expect you'll want to use more than one tag on an Article, I know I do.

Let's save the view.

This view created a block for us called "Related Articles", lets place it in the sidebar. Next, let's visit the articles and you will find the block in the sidebar. For more Drupal 8 related article click here.