Related category nodes in Drupal 9

The task is as follows - on the taxonomy page, you need to display a block of nodes from related categories.

Initially, the idea was to display nodes of neighboring categories (subcategories of the same parent) in the block if there was a missing number of nodes in the current category.

For this, a view was created with the output of nodes and a context filter for the identifier of the taxonomy term with depth

Контекстный фильтр

Create a custom category presentation template and add below the general code

{% if view.result | length < 20 %}
	<div id="cata_see_more">{{ drupal_entity('block', 'node_from_subcategory', check_access=false) }}</div>
{% endif %}

Here, first we check for the number of rows - nodes, if there are less than 20, in the block we display the presentation block with the machine name "node_from_subcategory".

The disadvantage of this option is that only a predetermined number of nodes are displayed in the view, and also that in the case of strict binding of nodes to categories, even in adjacent categories there may be not enough nodes + duplicates may appear.

The second option is to manually bind the necessary nodes in the taxonomy admin panel and display them on the category page.

For the second option, go to the management of the fields of the taxonomies we need and add a new field of the Link to Content type

Управление полей таксономии

We select an unlimited number of values and, in the link type, select the type of content that needs to be displayed. After that, when editing a category, autocomplete fields will appear, where you can bind it by the name of the node.

Now you need to display them in a block on the category page. We create a new view with the output of the content of the type we need with the output of the data to the block. First of all, go to the Links section and add a link

Связь представления

We add just such a link (with your category field name). If there is no such connection, then you have incorrectly specifiedðÊÉíU ðÊÉíU ЛÉÉíU  ™ÉÉíU XÊÉíU ÊÉíU @ ÊÉíU This link is required" checkbox.

After that, add the Taxonomy term: term ID context filter to the view, where we select - to transfer the term id value from the URL. In this case, the relationship filter must contain the machine name of our category field

Контекстный фильтр

Now you can add unfiltered text with a title to the view header, if needed, and remove the header output from the rendered view block. We do this so that at 0 results of this view (our additional nodes are not tied to the category), the block header is not displayed.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.
The comment language code.