Drupal 9: Using Taxonomy Terms To Create A Related Content Views Block

For the last few years I have been tagging articles as I write them on this site. This tagging has largely been to tie posts together in an aggregated list of other posts tagged with that term. I recently wondered if I could use those tags to show related content below each article. I have struggled with this feature on client websites in the past and it either boils down to a manually curated list or some sort of complex content analysis and Solr search.

As it happens this is fairly easy to accomplish using Views, although took some working out to get the effect I wanted. I'm writing down these instructions to help me remember how I did it in the past and to allow you set up the same thing (if you wish).

For some background, the structure of this site consists of an Article content type, which has a field called field_tags attached to it. This field is a free tagging field that allows me to tag posts as I am writing them and potentially create new tags as I need them. Creating new tags doesn't happen all that often after 10+ years of writing posts, but at least I have that option. There are currently about 300 unique tags, although most of them are either PHP or Drupal.

Using this setup I created the related content block by creating a View.

Create a new view using content data type. You can use the built in Views wizard to create a block containing the latest 5 published article items. This will set you up with a pretty standard view.

The related content is controlled through contextual filters, but there is one filter we need to add first. The related content block is meant to show other articles that might be of interest. We therefore need to add a contextual filter that will hide the current article being looked at. Showing the current article in the related content list is a bit pointless after all.

Add a contextual filter on the ID field of the Content category. Set the filter to provide a default value of the content ID from the URL. This will automatically pass the current page to the contextual filter.

View ID contextual filter default settings

At the bottom of this panel (in the More area) check the Exclude checkbox to prevent the current node from being included in the related content results.

View ID contextual filter exclude settings

Click Apply at the bottom of the panel to save this contextual filter.

Next, create another contextual filter, this time with the Has taxonomy term ID field in the Content category. Set the filter value to provide a default value and check the Load default filter from node page, that's good for related taxonomy blocks.". As a side note, I knew I was on the right track when I discovered this checkbox when doing the research for this feature.

In the multiple-value handling section select Filter to items that share any term. These options will tell Views to pull out the taxonomy terms from the current page and use them to look up any pages that contain any of the same items.

Term ID contextual filter default setttings

It's also a good idea to select the Reduce duplicates checkbox as this will prevent the same related item page being presented multiple times. This checkbox does come with a description of that it does so you need to be clear on that as well. Essentially, you need to make sure that the cardinality of your tagging is greater than one in order for this to work correctly. As my article tags are unlimited this isn't a problem, but there is also a warning not to add too many tags as this might slow things down. I never add more than 3-4 tags so I think this is safe enough.

Term ID contextual filter reduce duplicates

In the More section at the bottom of the panel click the Allow multiple values checkbox. Clicking the multiple values checkbox means that views can add lots of terms together to find the result. Note that the other option of Allow multiple filter values to work together is not compatible with the Reduce duplicates setting from above.

Term ID contextual filter more settings

Click Apply at the bottom of the panel to save this contextual filter.

Your contextual filters area should now look like this.

Views contextual filters list.

Everything else in the view is pretty much up to you. The formatting of the results or the sort orders are up to you really. I would suggest making sure that tag based caching is enabled (which it is in new Views).

After saving the view the last step is just to add the block to the article page. This is done through the normal block management pages. I won't post a screenshot here as can see this in action at the bottom of the page. I'm still playing around with different sort orders and formatting of the results, but I'm quite happy with the results so far. The list should show other articles tagged with "Drupal" or even "views".

Comments

Thank you for creating this tutorial. 

I was trying to get this to work using php code injection like how it worked in Drupal 7 but did not see the options in D9 so this really saved the day!

 

Permalink

You are very welcome Michael. Glad it helped out! :)

Name
Philip Norton
Permalink

If your page uses more than one vocabulary. In the Has taxonomy term ID contextual filter enable the "Limit terms by Vocabulary" checkbox and select the vocabulary you would want to use. Otherwise it will provide results for all the taxonomy terms on the page.

Permalink

Good tip! thanks Ravi :)

Name
Philip Norton
Permalink

Thanks!  This worked.  I was initially a bit confused/frustrated because the preview would not show the correct results.  User node/9 as the argument it tried to resolve content with a term-id of 9.  However, when I went to an actual content page, the results displayed were correct.  Maybe this issue was specific to my situation.  If not, perhaps this should be appended as a tip/note.   

Permalink

I followed the tutorial but my view isnt displaying anything at all... :(

Permalink

Thanks!  This worked. Remember preview is showing the right data or it may be blank, but the actual content page works great!!

Permalink

Hi!!

I have two vocalubaries, the Tags by default and another one! For the vocabulary Tags never show anything and for the another vocabulary works fine!Could you tell my something to help me?

Permalink

Hi spyridon, thanks for reading.

It should be a case of plugging the needed values in, but one thing springs to mind. If you add tags and categories to the same view filter then it might filter more than you expect. By default, views filters use AND so it will be searching for pages that have "Tag 1" and "Category 1".

What do you think?

Name
Philip Norton
Permalink

Hi Philip, I followed all the steps exactly as you mentioned, but I don't know why its not working out for me. In the View's query I can see that, the node id passed in contextual filter is being used as a term-id for filtering out nodes. Ideally it should grab out the taxonomy terms used in a node who's id is being passed as contextual filter... What could be wrong? Drupal version is 9.4.8 

Permalink

It Works Master. It works.   Yes  show me as wanted related nodes to current node, that as I want !

Permalink

Add new comment

The content of this field is kept private and will not be shown publicly.
CAPTCHA
14 + 2 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.