Used with the Menu block module, a parent menu item of a menu position rule show as "expanded" when it is active. It is confusing since there is no visible child items. It should show as "leaf".

Also, when the setting is mark the rule's parent item as being "active", the parent item should show as "leaf" too.

CommentFileSizeAuthor
screenshot3.png40.96 KBC. Lee
screenshot1.png29.45 KBC. Lee

Comments

C. Lee’s picture

You can reproduce this bug with Drupal 7.9, Menu block 7.x-2.x-dev, and Menu position 7.x-2.x-dev.

m4olivei’s picture

I'm also seeing this issue. I think the rule should be that if the only child of the menu item is the menu position rule, the parent should show as a leaf.

m4olivei’s picture

The issue may be beyond the grasp of menu_position, at least in its current state. Menu position adds an actual menu item for each menu postion rule to the Drupal menu system, which is what causes the parent item of a menu position rule to actually be rendered as a parent instead of a leaf.

Anyone have any suggestions for how we can render the parent item of a menu position rule as a leaf when its only children are the menu position rule?

m4olivei’s picture

For anyone struggling with the same issue, the way that I worked around this issue, which works quite nice in my use case, was to override theme_menu_link():

/**
 * Overrides theme_menu_link().  If the only child is a menu position rule, make it so its
 * rendered as a leaf.
 */
function mytheme_menu_link(array $variables) {
  $element = $variables['element'];
  $sub_menu = '';

  if ($element['#below']) {
    $sub_menu = drupal_render($element['#below']);
  }
  else {
    // Make sure the expanded class is not included.  This is for menu position rules mainly.
    if (!empty($element['#attributes']['class'])) {

      if (is_array($element['#attributes']['class'])) {
        foreach ($element['#attributes']['class'] as $key => $class) {
          if ($class == 'expanded') {
            unset($element['#attributes']['class'][$key]);
          }
        }
      }
      else if ($element['#attributes']['class'] == 'expanded') {
        unset($element['#attributes']['class']);
      }
    }
  }
  $output = l($element['#title'], $element['#href'], $element['#localized_options']);
  return '<li' . drupal_attributes($element['#attributes']) . '>' . $output . $sub_menu . "</li>\n";
}

This simple theme override catches the case where the only child is a menu position rule, simply by removing the 'expanded' CSS class that is present even when $element['below'] == FALSE

johnalbin’s picture

Title: Active parent menu item should show as "leaf" » Active parent menu item should show as "leaf" in menu_block

Yep. This is a known bug in menu_block module actually. Since I'm the maintainer of both modules, we can keep this issue open here until I get that fixed.

Bernsch’s picture

When you would show "leaf" in class attribut:

in #4 add this rows:
...
if ($class == 'expanded') {
unset($element['#attributes']['class'][$key]);
+ $element['#attributes']['class'][] = 'leaf';.
}
...
...
else if ($element['#attributes']['class'] == 'expanded') {
unset($element['#attributes']['class']);
+ $element['#attributes']['class'][] = 'leaf';
}
...

Bernsch’s picture

Status: Active » Needs review
andrewko’s picture

Since this is a known menu_block bug, is there a issue number we can link to in menu_block to cross-reference these two?

Anonymous’s picture

Status: Needs review » Active

The workaround from #4 doesn't add 'leaf' as a class (as #6 pointed out), so here's an updated and slightly simplified override of theme_menu_link() that does:

function MYTHEME_menu_link(array $variables) {
  $element = $variables['element'];
  $sub_menu = '';

  if ($element['#below']) {
    $sub_menu = drupal_render($element['#below']);
  }
  elseif (!empty($element['#attributes']['class'])) {
    if (is_array($element['#attributes']['class'])) {
      foreach ($element['#attributes']['class'] as $key => $class) {
        if ($class == 'expanded') {
          $element['#attributes']['class'][$key] = 'leaf';
        }
      }
    }
    else if ($element['#attributes']['class'] == 'expanded') {
      $element['#attributes']['class'] = 'leaf';
    }
  }

  $output = l($element['#title'], $element['#href'], $element['#localized_options']);
  return '<li' . drupal_attributes($element['#attributes']) . '>' . $output . $sub_menu . "</li>\n";
}

Here, instead of removing the 'expanded' class then adding the 'leaf' class, we just replace one with the other.

Bernsch’s picture

Status: Active » Needs review
mstrelan’s picture

Issue summary: View changes
Status: Needs review » Active

Workaround in #9 appears to do the trick, but since there is no patch I'm setting it back to Active.