2014 August

Remove Drupals .menu-position-link menu items (Menu Position module)

Menu Position is a great Drupal Module to get the .active-trail class in the menu, without adding eg. all news article nodes to the it.

The only problem is a „placeholder“ link created instead, class: „.menu-position-link“. It seems this is technically necessary.

Here is a little jQuery function to remove the element itself, the container menu (if its the only item) and fix the causing classes. You may have to alter it a bit.

Remove the console.logs before using it on productive environment!

$('.menu', context).children('li.active-trail').each(function() {
  var menuChildCount = $(this).children('.menu').children('li').length;
  var $menuPosLinkParent = $(this).find('.menu-position-link').parent();
  if ($menuPosLinkParent.length) {
    console.log('Found one - childcount:' + menuChildCount);
    console.log($menuPosLinkParent);
    if (($menuPosLinkParent).hasClass('last')) {
      console.log('Its the last item.');
      // If its the last link move class .last to the menu item before 
      $menuPosLinkParent.prev('li').addClass('last');
    }
    if (menuChildCount === 1) {
      console.log('Its the only item.');
      // If menu position link is the only menu-item, remove parent trails .expanded 
      $menuPosLinkParent.parents('li.active-trail.expanded').removeClass('expanded');
      $menuPosLinkParent.parent('.menu').remove();
      console.log('Removed it.');
    } else {
      $menuPosLinkParent.remove();
      console.log('Removed it.');
    }
  }
});