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

3rd January 2021 - 6 minutes read time

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

Permalink

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!

 

Michael Scott McGinn (Thu, 07/22/2021 - 21:35)

Add new comment

The content of this field is kept private and will not be shown publicly.