Creating a "Related Content" Block Using Views in Drupal 8/9/10/11, based on a common content reference

This guide will walk you through creating a "Related Content" block on the node view page of a content type in Drupal. The block will list other nodes of the same content type that share a common reference.

Definition:

To create a "Related Content" block using Views in Drupal 8/9/10/11, to be shown on the node view page of one item (node/entity) of content type A,
that lists other items (nodes/entities) of the same content type A which all share the same content reference to some other particular node/entity, as that on the initial item under consideration.

Example:

Context:

Imagine you have a Drupal site for a school with two content types: Student and Batch.
Each Batch node stores details of a batch, and
each Student node stores details of a student and includes a reference field (field_student_batch) to the student's batch.

Requirement:

When viewing a student's details, we want to display a "Batchmates" block listing other students in the same batch.

Implementation:

  1. Create the View:
    • Name the view "Batchmates".
    • Show Content of type "Student", sorted by "Oldest first".
    • Select the "Create a Block" option.
    • Set the "Display Format" to "Table" of fields.
    • Set "Items per block" to 0 (to show all items).
    • Save the view.
  2. Edit the View:
    • Navigate to the view's edit page.
    • Under "Advanced" > "Relationships", add these two relationships in the given order:
      • "Content referenced from field_student_batch" (Category: "Content").
        Change the Administrative title to "Batch the student is in"
        and click Apply.
      • "Content using field_student_batch" (Category: "Content").
        Change the Administrative title to "All students in that batch".
        Set the Relationship to the previously added "Batch the student is in"
        and click Apply.
    • Under "Advanced" > "Contextual Filters", add these two contextual filters in the given order:
      • "ID" (Category: "Content").
        Set the Relationship to "All students in that batch".
        Under "When the filter value is NOT available" > "Provide default value", select "Content ID from URL".
        Set the Administrative title to "Show students in that batch"
        and click Apply.
      • "ID" (Category: "Content").
        Set the Relationship to "Do not use a relationship".
        Under "When the filter value is NOT available" > "Provide default value", select "Content ID from URL".
        Set the Administrative title to "Do not show the student whose page we are on".
        Under More, select "Exclude"
        and click Apply.
  3. Test the View:
    • Use the "Preview" area on the view's edit page.
    • For "Preview with contextual filters:",
      input a student node's id twice, separated by slash, in the format "StudentPageNodeId/StudentPageNodeId"
      (e.g., if the node ID is 5, input "5/5").
    • Click "Update preview" and ensure the list shows the correct batchmates.
    • Save the view.
  4. Place the Block:
    • Go to 'Manage > Structure > Block Layout'.
    • Place the "Batchmates" block in a region of your choice.
    • In the "Configure block" dialog, restrict the block to show only on Student content pages by selecting the Student content type under "Configure block > Visibility > Content type".

By following these steps, you'll create a "Batchmates" block that displays relevant students when viewing a Student node page.

Add new comment

randomness