You are here

Create related content block in Drupal 7 using Contextual filter

Submitted by Asif Nowaj, Last Modified on 2019-11-22

If you are looking for “how to create a related content block in Drupal 7”, you are at the right place. I have successfully created a block for displaying related content using Taxonomy, Contextual filter with views.

You can check out right sidebar at the top of my website is showing all my contents related with PHP.

Now I am going to explain step by step how I have created related content blocks in Drupal 7 with views.

I am assuming you are already logged in as administrator.

Check whether your taxonomy core module is enabled. To check that follow the below instruction.

1. Go to “Modules” => Look at the Core Modules named “Taxonomy” whether it is ticked. [You can rest assure that it is ticked for Drupal 7 by default.]

Taxonomy module enables you to categorise your contents.

2. Now go to “Structure” => “Taxonomy”. Then add a vocabulary let’s a “Technology”. It can be like “Topic” or you can use any string to categorise your content.
Taxonomy

3. Add terms into vocabulary like “Delphi”, “SQL Server”, “PHP” etc.

Terms are grouped into vocabularies. For example, a vocabulary called "Fruit" would contain the terms "Apple" and "Banana".

I have added below terms for my websites.
Taxonoly Term List

4. Go to “Structure” => “Content types”; decide for which content type, you want to apply the taxonomy that you have just created above.

I have selected many content types but let’s take one for simplicity named “Articles” that I have.

5. Then go to “manage fields” of the selected content type. Explore “Add existing field” dropdown and find your term reference field. In my case it is “Term reference: field_technology (Technology)” and select “Select List” as Widget and Save. It should look like below:
Content Type Existing Field Add

6. Then you can go to your existing content of the above content types from “Find Content” and add the proper vocabulary/term and save. Our intention would be to see these contents as a list in the “Related Content” block.

7. Now go to “Structure” => “Views” and select in which view you want to generate the block.
I have selected view called “Technicals”.

Go to “Edit” of the selected “View” and add a block with display name “Related Content” or provide any name that you want to show as heading of the block. Use to display no title, or leave blank to use the default block title.
Add Block In Views

I have created it with named “Related Content”.

8. Now we need to use “Contextual Filters” and “Relationship” to get the filter the related content links. Explore “Advanced” option of the newly created block “Related Content”.

a. Add a relationships with “Content: Taxonomy terms on node”, apply and then select proper “Vocabulary” with below details.
Add Relationship

Then “Apply (this display)”. Then follow the below settings.

Add Second Relationship

If you have given different vocabulary name, select the same.

b. Based on this relationship, we have to create another relationship. Add another relationship with “Taxonomy term: Content with term”.
Add Second Relationship

Then “Apply (this display)”. Then follow the below settings.
Add Second Relationship

Then “Apply (this display)”.

9. Now time to add a “CONTEXTUAL FILTERS”. Click Add. Select “Content: Nid” which is the node id. As Below “Apply (this display)”.

CONTEXTUAL FILTERS

Then select the following settings next.
CONTEXTUAL FILTERS

Then “Apply (this display)”. It should look like below.
Related Content Block

10. We are almost there. Now go to “Structure” => “Blocks”
Block

Set “Region settings” where you want to show your block in your website. I have selected “Sidebar Second”.

Change the visibility setting as per your requirement. I have selected as below:

Pages => All pages except those listed

Content types => I have selected few of my content types for which I want this block to be visible.

I haven’t done any role restriction and no user restriction.

11. Now logout and open a content that you setup the terms, you will be able to see the related contents link at the top right hand bar.
Related Content Outcome

Only issue here is, “Related Content” will be shown the current article’s link as well. If I find how to remove this one, I’ll write another post. Please leave your comment to improve the article.

Discussion or Comment

If you have anything in mind to share, please bring it in the discussion forum here.

https://forum.everyething.com/php-f36/