Drupal Views Related Taxonomy

By Xandermar LLC, September 12, 2023

To create a Drupal 9 View that displays related articles that have the same taxonomy terms as the current node, you can follow these steps:

  1. Install and Enable Required Modules:

Ensure that the following modules are installed and enabled:

  • Views: This module is essential for creating and displaying views.
  • Taxonomy: Required for managing and associating taxonomy terms with content.
  1. Create a Taxonomy Vocabulary:

If you haven't already, create a taxonomy vocabulary that you'll use to categorize your content. For example, you can create a vocabulary called "Tags."

  1. Add Terms to the Taxonomy Vocabulary:

Populate the vocabulary with terms (tags) that you want to use to categorize your content. For example, you can create terms like "Technology," "Science," "Sports," etc.

  1. Assign Taxonomy Terms to Content:

Edit your articles or content types and assign the relevant taxonomy terms to them.

  1. Create a View:
  • Go to "Structure" > "Views" > "Add view" to create a new view.
  • Set the View name, and choose "Content" as the Show type.
  1. Add Relationships:
  • Under the "Advanced" section, click on "Add" next to "Relationships."
  • Search for and select "Content: Taxonomy terms on node."
  • Choose the vocabulary you created earlier (e.g., "Tags") and click "Apply and continue."
  1. Add Contextual Filter:
  • In the "Advanced" section, click on "Add" next to "Contextual filters."
  • Search for and select "Content: Nid" (Node ID).
  • Configure the filter settings:
    • In the "When the filter value is NOT available" section, choose "Provide default value."
    • Select "Content ID from URL" as the default value.
    • In the "When the filter value IS available or a default is provided" section, choose "Specify validation criteria."
    • For "Validator," choose "Content."
    • For "Content type," select the content type(s) you want to include in the view.
    • Under "Filter value type," choose "Term ID" to pick up the taxonomy term from the current node.
    • Select your vocabulary (e.g., "Tags") as the "Vocabulary."
  1. Add a Relationship for the Taxonomy Term:
  • Go back to the "Advanced" section, click on "Add" next to "Relationships" again.
  • Search for and select "Taxonomy term: Content with term."
  • Choose the vocabulary you created (e.g., "Tags") and select the relationship you created earlier (e.g., "Taxonomy term from node").
  1. Add Fields:
  • Under "Fields," click on "Add" to add fields related to your articles that you want to display (e.g., Title, Body, etc.). Make sure to use the relationship you created in step 8 to pull in fields related to the same taxonomy term.
  1. Configure Filters:
- Add a filter for "Content type" if needed, so you only display related content of specific types.
- Add a filter for "Published" if you want to show only published related content.
  1. Configure the View's Display:
Configure the settings for your View's display as needed. You can choose to display related articles as a list, grid, or any other format that suits your design.
  1. Save and Test:
Save your View, and then test it by visiting the page where you've placed the View block. You should now see a list of related articles that share the same taxonomy terms as the current node.

By following these steps, you can create a Drupal 9 View that displays related articles based on shared taxonomy terms with the current node.

Tags

Comments