0

I am trying to create a content type that generates a list of content/nodes based on other content type or taxonomy terms selected within that node in the content type. I have a general content type for Articles and those are tagged with a taxonomy vocabulary for "Category".

This is within Drupal 9.

What I've done so far:

Create a new content type named "Custom List"

Add a field for an entity reference for Taxonomy Terms. Set the field to unlimited and selected the taxonomy vocabulary for Category. Then set the field to display as radio/checkboxes.

Created a View for Custom Lists as a Block. Set the block to display only on Custom List content types.

Set the View to list 10 items, with a contextual filter for Taxonomy Term ID, set to display all results.

What I want to happen:

When I create a new node for this Custom List content type, I will see the Categories taxonomy field I added as a list of checkboxes. I can check any number of taxonomy terms from this Categories vocabulary and the page will then display a content list of content that are only tagged with the terms I selected within this node. For example if I selected "News" and "Blog" as my terms, the list will only show content tagged with Category > News and Category > Blog.

Eventually I will be able to select Content Types as a filter on top of the taxonomy terms, but for now Im just trying to get the taxonomy filter working first.

What is actually happening:

The list generated still displays every content regardless of the term I selected when I created or edit the node. I have tried many things such as adding relationships from the Taxonomy Entity Reference field (Category) as well as the taxonomy term itself but to no avail.

Can someone help me with this issue or suggest a better or easier solution? Most likely my View isnt setup correctly as Im very inexperienced with contextual filters. I'm not even sure if I can achieve what Im trying to do with Contextual Filters. If there are any modules out there that can do what Im trying to do please advise or suggest them as well. Thank you all for your time.

1 Answer 1

0

For the Content Type side, you've already done what needs to be done. In summary, a Content Type with an Entity Reference field pointing to Taxonomy Terms.

The Views side is where it gets tricky, but here's what you need to do:

  1. You need a View of Nodes filtered by your "Custom List" content type.

  2. You need to add a relationship from your Custom List nodes to the Category terms. To do this, add the relationship named:

    Taxonomy term referenced from field_category
    

    In the relationship window, update the Administrative Title to "Categories" so that it's less confusing. It will help in the next step.

  3. Here's the tricky part: You need to add a relationship to all the Nodes that are using your Category taxonomy. To do this, add a relationship called:

    Content using field_category (Relate each Content with a field_category set to the taxonomy term.)
    

    In the relationship window, update the Relationship field to "Categories" (the name we set in Step 2). Also update the Administrative Title of this relationship to "Viewed Node". It will help in the next step.

  4. To round things off, you need a contextual filter to filter "Viewed Node" to the current node's ID. To do this:

    • Add a contextual filter. Filter by "Content", select the field named "ID".
    • Under Relationship, choose "Viewed Node" (the name we set in Step 3).
    • Under When the filter value is NOT available, select "Provide default value" and set the Type to "Content ID from URL".
  5. Place the view block on your content type's page.

In summary, what the above did was link nodes to the terms, then terms back to nodes. Then, we just filtered the linked nodes to the current node. In SQL, it's something like the following pseudocode:

SELECT *
FROM nodes AS custom_list
INNER JOIN terms AS categories ON categories.id = custom_list.categories
INNER JOIN nodes AS viewed_nodes ON categories.id = viewed_nodes.categories
WHERE viewed_nodes.id = current_node.id
     

Or visually:

Nodes that appear on the view block
                v
            List nodes <- Category terms <- Nodes using the terms
                                                    ^
                                   Filter these by the current node's ID
1
  • Thank you so much! These steps were really easy to follow and you even explained the reasoning behind each which was very helpful. One thing I had to edit was Step #1, I had to leave the filter blank as content being pulled are different content types than "Custom List" but once that was removed it works like a charm. Thanks again you rock!
    – JohnC95
    Mar 28, 2023 at 15:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.