I managed after several attempts to get the 'parent' of a taxonomy term. It works, but I didn't understand if it might be the most correct method. I modified the previous code that showed only the term, so I suppose that the {% for item....} part may be unnecessary.

{% if node.field_main_taxonomy_term.entity.parent %}
{% apply spaceless %}
<div class="main-tag">
    {% for item in node.field_main_taxonomy_term %}
   {% set href = path('entity.taxonomy_term.canonical', {'taxonomy_term': item.parent.target_id}) %}
   <a href="{{ href }}">{{ item.entity.parent.entity.getName }}</a>
  {% endfor %}
  </div>
{% endapply %}
 {% endif %}

But my real goal was to get in some sections both the parent and the term(s) itself.

Intuitively, all I should do is insert the two 'if' blocks, one for the parent term, and one for the terms, but I wonder if there is a more concise and elegant way that I cannot retrieve from the documentation.

Thanks to those who would like to point me to the most Drupal-like way.

Comments

wombatbuddy’s picture

Could you provide more info about the use case please. Also, it's interesting to know the configuration of taxonomy vocabulary (how many levels it has).

senzaesclusiva’s picture

Hi @wombatbuddy, thank you for your replay.

The vocabulary structure, I deliberately designed it very simple (only two levels, each parent - some child terms) giving preference to breaking down the topics (it's a news site) into more specific parent-topics rather than creating too much nesting. Eg:
climate change
-bad weather
-drought
-temperatures
-extreme phenomena

 All further categorized (with another vocabulary, and another field) with which to indicate the State.

The use case is also very simple. Indicate to the user who is reading an article, eg temperatures moskow (more news about "temperatures moskow" will be found in a view on the same page as the news story) that if he or she wants there is much broader news in the topic-parent climate change.
Then show on the page the tags:
climate change temperatures (1st Vocab) moskow (2nd Vocab).

The reason is simple. Much research indicates that mobile users "like little" to use the off-canvas menu unless they are looking for something very specific. It is therefore much more intuitive and user-friendly (as well as helpful to the publisher for retention) to explicitly suggest main navigation paths

PS: Alternatively I could have opted for custom breadcrumbs, but if possible I prefer to avoid adding modules and place useful information where it is best needed

Thanks again for your interest. I wish you a good day 

wombatbuddy’s picture

{# If a term has parent, then render the link to the parent term. #}

{% if node.field_main_taxonomy_term.entity.parent.target_id > 0 %}
  {% set parent_term = node.field_main_taxonomy_term.entity.parent.entity %}
  {% set href = path('entity.taxonomy_term.canonical', {'taxonomy_term': parent_term.id}) %}
  <a href="{{ href }}">{{ parent_term.getName }}</a>
{% endif %}

{# Render the link to the child/parenless term. #}

{% set term = node.field_main_taxonomy_term.entity %}
{% set href = path('entity.taxonomy_term.canonical', {'taxonomy_term': term.id}) %}
<a href="{{ href }}">{{ term.getName }}</a>

Or render a child/parentless tag like this: 

{{ content.field_main_taxonomy_term }}
senzaesclusiva’s picture

Thanks @wombatbuddy, all much clearer than how I had set it up

Many thanks