Site visitors would like to see content related to a specific state/territory. To do this, we need to set up an automatically generated list of States/Territories in a block with the following features:
When we click on a term such as “ACT” we go to a term listing page that shows all content tagged or categorised with that term.
If we then select a specific article such as “Volunteering” we see the full content page.
In the sidebar of the “Volunteering” full node page we also see a “related content” block. This will pull in a list of content that’s been tagged or categorised with the same taxonomy term.
In the following exercises we'll make a “list of terms” block and then we'll make the related content block.
Exercise 6.5: Make a list of States/Territories block
In this exercise we’ll add a list of the States and Territories in the sidebar, so the page looks like the screenshot below.
Go to Structure → Views. Click + Add new View. Add in these settings:
View name: States/Territories
Select to enable Description: A list of State and Territory terms
Show Taxonomy terms of type State/Territory sorted by Unsorted
Select Create a block
Block title: States/Territories
Display format: HTML list of Fields
Items per block: 0
Use a pager: Leave unselected
Click Save and edit
Go to Structure → Block Layout.
In the Sidebar area click the Place block button.
Find States/Territories block in Lists (Views) category and click the Place block button.
Configure block visibility settings to only display it on the Job Postingcontent type.
Click Save block.
Navigate to a Job posting content page and locate your block.
Exercise 6.6: Build the related content block
Next we’ll add the related content block, as per the screenshot below.
Check content tags
Make sure you have some content tagged with the State/Territory terms.
Create the View
Click Save and edit.
Add dynamic "contextual" filter
The contextual filter allows us to show a list of the content with the same tags as the current content.
Place the block
Go to Structure → Blocks and scroll down to the “Sidebar” area
Click the Place block button.
In the pop-up, filter by text “Related content” in the Lists (Views) category.
Under Visibility settings, configure block to display on all pages except the homepage. Remember to use the <front> token as the homepage identified. You may also use “/”.
In addition, filter the block to only show on the following content types:
Click Save block.
Review and test
Go to the frontend of your site and check that the Related content block has been added, as in the below screenshot.
Check that you have some content in certain states/territories, so you can see how the block works.
When you click on a state (tag) on a specific article you should see a list of related content, listed by the taxonomy term. You should not see the block.
You should see the Related Content block when you view a job posting or News and Media article.
Filter out currently displayed page
While testing the Related content block, did you notice anything unusual?
The Related content block displays the article title of the currently displayed page:
Let’s remove it by adding one more Contextual Filter.
Return to editing the Related Content view.
Expand the Advanced pane and you should see the Contextual FilterContent: Has taxonomy term ID that we created earlier.
Add another contextual filter
Search for ID.
Select Provide default value in the When the filter value is NOT available pane → Content ID from URL.
Under the More pane, select Exclude
Apply the changes.
Save the View.
Test your block from the previous example. Now it shouldn’t display the current page in the block.