Hide unrelated content and taxonomy terms (high performance version)

Created on 30 May 2021, about 3 years ago
Updated 11 June 2024, 14 days ago

Problem/Motivation

On my site I use a taxonomy vocabulary to give each department its own space. On each department's site, I want the navigation menu to show only the current department's content, not content belonging to other departments.

I'm using Taxonomy Menu to generate a menu based on the Departments taxonomy and I want to use Menu Block to add the menu to the pages. However, Menu Block doesn't currently provide a way to filter out unrelated content and taxonomy.

Two caveats:

  1. Departments can be nested inside each other if a department has sub-departments. The navigation menu should show the entire taxonomy structure so end users can navigate it. It should be able to show as many levels as needed.
  2. Content can be nested under other content. For example, a committee page that has the current year's agendas on it might need to keep past years' agendas available, but those would be listed on a separate page under the committee's page.

Here are a couple visual examples of how the navigation menu should work.

Example 1:

  • Menu
    • Term 1
      • Content 1
      • Content 2
      • Content 3
        • Content 3a
    • Term 2
      • Content 4
      • Content 5

Expected result:

  • Menu
    • Term 1
      • Content 1
      • Content 2
      • Content 3
        • Content 3a

Example 2:

  • Menu
    • Term 1
      • Term 1a
        • Content 1
        • Content 2
        • Content 3
          • Content 3a
      • Term 1b
        • Content 4
        • Content 5
    • Term 2
      • Content 6
      • Content 7

Expected result:

  • Menu
    • Term 1
      • Term 1a
        • Content 1
        • Content 2
        • Content 3
          • Content 3a

Proposed resolution

After spending a few days experimenting with it, I've created a working solution. It appears to support as many menu levels as might be needed, and to the best of my knowledge doesn't contain any hacks and is compliant with Drupal's coding style guide.

All that said, I don't consider myself a backend developer. I spend most of my time as frontend developer, and as such I'm certainly not as familiar with Drupal's backend as many of the members of this community. If anyone sees any issues with this solution or sees a way that it could be done better, please let me know and I'm more than happy to either rework it myself, work together to fix it, or let someone else do it.

Note: My solution started out using the accepted solution posted at https://drupal.stackexchange.com/questions/183201/get-menu-link-siblings as a reference, but I simplified it.

User interface changes

Adds a checkbox under Advanced Options in the menu's block configuration labeled "Show current taxonomy term content and active taxonomy hierarchy."

API changes

Adds (in src/Plugin/Block/MenuBlock.php):

  • A new form control called show_current_term_content in the blockForm function
  • A call to set the show_current_term_content configuration setting in the blockSubmit function
  • A variable in the build function to get the value of the show_current_term_content configuration setting
  • A call to a new function called removeUnrelatedContent in the build function (the function is only called if the show_current_term_content setting is true)
  • A setting in the defaultConfiguration function to set the show_current_term_content setting to false by default
  • A new function called removeUnrelatedContent that recursively removes content and taxonomy terms from the menu tree that aren't in the current taxonomy term's menu tree.

Data model changes

Adds a new Boolean configuration setting called show_current_term_content to store the setting for this option.

✨ Feature request
Status

Needs review

Version

1.11

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States jsutta United States

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.69.0 2024