Problem/Motivation

Menus don't respect unchecking the "Show as expanded" option when you're viewing a page in the active trail, in those cases, the menu item is treated as though you had checked "show as expanded."

Steps to reproduce

  1. Add menu item and set show as expanded to false.
  2. Add sub menu items to the menu item.
  3. Navigate to some random page with the menu and see that the menu item is not expanded.
  4. Navigate to a page in the active trail, i.e. a page for one of the sub menu items and see that the menu item is treated as expanded even though it was not configured to be "shown as expanded"

This seems to be very similar to #3055340: Menu item checkbox "Show as expanded" not working properly, probably a duplicate.

Proposed resolution

It seems that this is a hardcoded decision that if viewing a page in the active trail, all parent menu items are shown as expanded as long as they are within the max-depth requirements of the menu. But should this be the case? It seems like it might be a more intuitive behavior to only expand the items if "show as expanded" is checked. (Or if the menu block is forcing all items to be "shown as expanded".)

Remaining tasks

  1. Update code to respect the "show as expanded" option, even on pages in the active trail.

User interface changes

API changes

Data model changes

Release notes snippet

Issue fork drupal-3456536

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

Ibrahim Tameme created an issue. See original summary.

ibrahim tameme’s picture

Title: show as expanded is forced to true if menu item has children and you are on child trail » Show as expanded is forced to true if menu item has children and you are on child trail

ibrahim tameme’s picture

StatusFileSize
new711 bytes
ibrahim tameme’s picture

StatusFileSize
new978 bytes

After checking it turn out that all set as expanded option in Administration menu links are set to false, so with the patch provided above you will need to update all the Administration menu links which is a rough task so this patch ignores the Administration menu and add the condition to other menus.

ibrahim tameme’s picture

StatusFileSize
new976 bytes
arun.k’s picture

@Ibrahim Tameme i installed drupal 10.3.x on DDEV v1.22.6 and php 8.3.0 and apply the patch 3456536-fix-menu-expansion-7.patch which is working fine.

quietone’s picture

Version: 10.3.x-dev » 11.x-dev
Issue summary: View changes
Issue tags: +Bug Smash Initiative, +Needs issue summary update
Related issues: +#3055340: Menu item checkbox "Show as expanded" not working properly

This may be a duplicate of #3055340: Menu item checkbox "Show as expanded" not working properly.

I tested this on Drupal 11.x, using the steps in the issue summary. I did not see any difference in the UI between when the patch was not applied and when it was. Is there supposed to be a change?

Changes are made on on 11.x (our main development branch) first, and are then back ported as needed according to our policies.

aaronchristian’s picture

This seems to fix the issue for me.

Previously {{ item.is_expanded }} was being forced to true when on the active menu item.

Debugging it reveals false when on the active menu item and it has children, even when its not set to "expanded".

Expanded - True

After applying the patch;

Expanded - False

And then just to ensure your menu doesn't print out, ensure you have some logic like;

{% if item.below and item.is_expanded %}
  {{ menus.menu_links(item.below, attributes, menu_level + 1, item) }}
{% endif %}

Thanks for the patch!

sonfd’s picture

Issue summary: View changes
sonfd’s picture

Issue summary: View changes
sonfd’s picture

I tried to update the issue summary to be a little more clear. I do think this is a duplicate of #3055340: Menu item checkbox "Show as expanded" not working properly, but I think the approach in this issue is the correct way to go so I'd recommend closing the other one.

To @aaronchristian's point, I think this patch is only complete with a change to the template file. Maybe I'm wrong, but I think we shouldn't even be building the subtrees of a menu item if it's not expanded. And if we do that, we won't need a change to a template file. I'll take a stab at that in an MR now.

mortona2k’s picture

They read as duplicates to me, both are talking about confusion over the option and how it affects hardcoded behavior.

I was recently diving into how menus load, and learned about using MenuLinkTree service to load the menu. At first I was trying to stuff extra content into the items with a custom modifier for MenuLinkTree->transform(). But then I discovered that those are more for controlling the structure of the menu, like removing links you don't have access to, or changing the order. But loading additional content is intended to be in a preprocess.

I took notes on things I discovered here: https://www.drupalarchitect.info/articles/rendering-menus

I think the question is where we should make the decision on how menus render. If everything is available, the decision can be made in the template. If the child links are removed further back in the render pipeline, they take deeper development to modify. For example, adding a custom handler to adjust the link loading takes way more effort than deciding not to print level 3 items in a template file.

That said, I think default menu templates should be able to handle whatever links are passed in, and the UI should give us clear control over what is happening per menu display.

sonfd’s picture

Actually, scratch pretty much everything I just said.

I'm seeing in MenuLinkTree::getCurrentRouteMenuTreeParameters(), we are explicitly expanding links in the active trail:

/**
   * {@inheritdoc}
   */
  public function getCurrentRouteMenuTreeParameters($menu_name) {
    $active_trail = $this->menuActiveTrail->getActiveTrailIds($menu_name);

    $parameters = new MenuTreeParameters();
    $parameters->setActiveTrail($active_trail)
      // We want links in the active trail to be expanded.
      ->addExpandedParents($active_trail)
      // We marked the links in the active trail to be expanded, but we also
      // want their descendants that have the "expanded" flag enabled to be
      // expanded.
      ->addExpandedParents($this->treeStorage->getExpanded($menu_name, $active_trail));

    return $parameters;
  }

If we're going to do something here, I think we have to do it there or change the behavior in SystemMenuBlock, which is where this gets called from.

mortona2k’s picture

Doing it in SystemMenuBlock makes sense to me, that's where the "Expand all menu links" checkbox is. An additional checkbox for "Don't auto-expand items in the active menu trail"?

sonfd’s picture

@mortona2k, I was just coming back to this after sleeping on this to say basically the same thing! Except I think I'd recommend the additional checkbox be "Expand items in the active trail" and we can default it to checked.

mortona2k’s picture

That makes more sense.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.