Score:0

Views Jump Menu containing only sibling taxonomy terms

nr flag

I'm using Views Jump Menu with Drupal 9.

Nodes are tagged with a software_version hierarchical taxonomy, following this pattern:

├── foo
│   ├── 0.1.0
│   └── 0.2.0
├── bar
│   ├── 0.3.0
│   ├── 0.3.1
│   └── 0.4.0
└── baz
    ├── 0.1.0
    ├── 0.2.0
    └── 0.3.0

The plan is for nodes of a custom Content Type to be tagged with a child term from the list above (the numerical values), generating a jump list containing only the nodes tagged with sibling terms that share the same parent (i.e., only the documentation pages for different versions of the same software).

Under /admin/structure/views/view/taxonomy_jump_menu/edit, there's a Block display of a View which currently returns the entire taxonomy vocabulary, with all parents and children. In other words, the Jump menu links to documentation for all versions of all the different software.

I've been hoping that there's a way in Views to add a Contextual Filter which could determine the taxonomy term TID of the current node from the URL, use that to determine the TID's immediate parent TID (of which there should only ever be one) and generate a jump menu containing all the children of the parent TID, excluding the parent term itself.

The config for this View is hundreds of lines of YAML, but here is an excerpt (with many irrelevant lines deleted, but indentation preserved):

dependencies:
  config:
    - taxonomy.vocabulary.software_version
  module:
    - taxonomy
    - user
    - views_jump_menu
id: taxonomy_jump_menu
label: 'Taxonomy Jump Menu'
module: views
description: ''
tag: ''
base_table: taxonomy_term_field_data
base_field: tid
display:
  default:
    display_options:
      query:
        type: views_query
      style:
        type: jump_menu
      row:
        type: fields
      fields:
        parent_target_id:
          id: parent_target_id
          table: taxonomy_term__parent
          field: parent_target_id
          relationship: none
          group_type: group
          exclude: true
        tid:
          id: tid
          table: taxonomy_term_field_data
          field: tid
          relationship: none
          group_type: group
          exclude: false
          alter:
            alter_text: true
            text: '/taxonomy/term/{{ tid }}'
        name:
          id: name
          table: taxonomy_term_field_data
          field: name
          relationship: none
          group_type: group
          exclude: false
      filters:
        status:
          value: '1'
          table: taxonomy_term_field_data
          field: status
          plugin_id: boolean
          entity_type: taxonomy_term
          entity_field: status
          id: status
          expose:
            operator: ''
            operator_limit_selection: false
            operator_list: {  }
          group: 1
        vid:
          id: vid
          table: taxonomy_term_field_data
          field: vid
          value:
            software_version: software_version
          entity_type: taxonomy_term
          entity_field: vid
          plugin_id: bundle
          expose:
            operator_limit_selection: false
            operator_list: {  }
          group: 1
      sorts: {  }
      title: 'Taxonomy Jump Menu'
      relationships: {  }
      arguments: {  }
      filter_groups:
        operator: AND
        groups:
          1: AND

There's plenty more where that came from, but I'm trying to include only salient details!

I do not think this question has a duplicate answer on this forum. The closest I have found are this question which pertains to menus rather than taxonomy (although maybe my problem is that I should be using a Taxonomy Menu here), and this unresolved question which does not include enough detail to know whether it's relevant.

I have also checked the issue queues for the contrib module, but it really seems to be a limitation of Views itself, which is in core.

I have tried adding Configure contextual filter: Taxonomy term: Term Parents to the View, but that unexpectedly generates a jump menu containing only parent terms:

├── foo
├── bar
└── baz

At this point, I am starting to think that it will be much easier to write a hook implementation that will modify the query, but this is a hard topic to research online. (My search queries turn up a lot of irrelevant issue queues going back to Drupal 5.)

So, to boil this all down to an answerable question, what hook implementation would be best suited for this task?

I'm leaning toward using hook_views_query_alter() to modify the $query object itself (within $query->where) based on this comment, but I'm in pretty deep water here.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.