Expanding a menu when not on a child page

futurist - September 4, 2007 - 15:40

Is there any way to keep a menu expanded (so that all of its items are shown), even if one is viewing a page that is not a child of that menu? For example:

Menu
- Item 1 (URL 1)
- Item 2 (URL 2)

Search page (URL 3 - currently viewing)

Items 1 and 2 of Menu should be visible when viewing URL 3. Can this be solved by a custom function in template.php? I've seen that a URL can be mapped to a function without creating a visible menu item but I don't know if that can solve this problem.

Thanks for any help!

Expanding a menu

zeta ζ - September 4, 2007 - 16:36

If you edit a menu item – the parent of the items that you want to be shown – click the tick-box marked Expanded and save (submit) it, you should get the desired effect.
NB this doesn’t work for displaying the children of a #primary menu item in the #secondary menu.

The problem is more complex...

futurist - September 4, 2007 - 17:33

Thanks zeta-zoo. I see that I need to describe my situation in more detail to show the problem. I have:

Shop
- Guitars
- Amps
Service
- Guitar Repair
- Amp Repair

Search page (currently viewing, not in menu, "Shop" should be expanded)

When I follow your suggestion ("Shop": expanded), then its children "Guitars" and "Amps" always show, even when I go to "Service". (When setting the parent item "Service": expanded at the same time, the children "interfere".)

However, what I need is:

  1. Children of "Shop" expanded when: viewing Shop, Guitars, Amps or the Search page (which is not in any menu).
  2. Children of "Service" expanded when: viewing Service, Guitar Repair or Amp Repair.

Try this...

zeta ζ - September 5, 2007 - 01:14

Add another item as a child of Shop, that points to the Search page — then disable it.
Success might depend on the exact address of the Search page, but if there is no other items in that menu that are similar, it should work.

Doesn't work...

futurist - September 5, 2007 - 06:44

I tried — but it doesn't work, even though I set the path exactly to the address of the page (including slashes). (Then, I'm not surprised it doesn't work; after all the item is disabled.)
Maybe there is a different approach to this using an override function in template.php? Or a module (perhaps Menu Trails)?

Don't give up...

zeta ζ - September 5, 2007 - 14:04

I’m not guessing… I have tried it, and it does work :o

Unless otherwise noted, all the suggested solutions that I post, have been tested[1][2].

Being disabled is not the problem here.

  • Did you set the path to node/search (or whatever points to the search page)?
  • How is the search page created; by a node or a module?
  • Does the search page have a menu item in another menu, pointing to it?
  • If not, have you tried editing the search page node, to add the disabled menu item?

I don’t see the point of using an override function, or a module when the menu system is already capable of doing this.
    ——————————————

  1. To a reasonable extent.
  2. I don’t always understand the problem :(.

Didn't give up — and finally got it working

futurist - September 5, 2007 - 20:27

Thanks a bunch, zeta-zoo! These were some very useful points to look at:

How is the search page created; by a node or a module? It is created by the Views module. The view provides a page, and I had to tell it to also provide a menu entry. I set the path to search_product in the view.

Does the search page have a menu item in another menu, pointing to it? No, the search page doesn't. The view just exposes a filter (search form) in a block. But I have other pages (some created by views, some by nodes) which have menu items in another menu (call it Submenu) and which should make the Shop menu expand. For those pages, this is what happened:

Add another item as a child of Shop, that points to the {page XY} — then disable it. I created an entry called {Page XY} (Dummy) and disabled it. This alone didn't work. I had to delete the previously existing entry that points to the {page XY} in Submenu and then re-create it exactly as it was! What difference does that make? The newly created entry in Submenu has a different id (before: 165, now: 260). The id of the disabled dummy in the Shop menu is 259.

My bottom lines:

  • Adding an item to a menu makes that item (and its URL) a child of that menu, no matter whether the item is enabled or disabled. That's why the menu expands when the URL is viewed.
  • If a page has menu items in two menus pointing to it, with one of the items disabled, then the menu with the disabled item only expands if the id of that item is lower than the id of the enabled item.

Can this be true? (#2 looks eerie to me...)

P.S.: Never meant to suggest you were just guessing — I guess I was just a bit desperate. Very much appreciate your help.
P.S.2: I don’t see the point of using an override function, or a module when the menu system is already capable of doing this. I absolutely second that. Knowing Drupal only since a couple of months, I just find it very difficult sometimes to determine what it can do out of the box (e.g.: can images be inserted into blog posts? — not with core modules), or whether it's better to plug in a module (and learn how to use it) or write some custom code in template.php (which i'm often more comfortable with).

“∴ —”…

zeta ζ - September 5, 2007 - 22:03

Cool. Glad I wasn’t going mad.

#2 does look a bit weird, but could be due to iterating through a list and assuming (understandably) that there’s only one. Will have to look at the code.

I’ve been trying for months to put entities in the title of these comments – why didn’t I think of copy&paste before? Is that how you did it?

:—)

futurist - September 6, 2007 - 06:56

Will have to look at the code. Let me know what you find, please. Btw. the menu items provided by views don't seem to interfere with disabled dummy menu items the same way manually created entries do.

copy&paste? Yes!

Couldn’t find anything

zeta ζ - September 7, 2007 - 20:50

Couldn’t find anything in the code but these might be of interest…

Child menus
Expanded menus don't stay expanded
Expanding a menu tree based on node types

It is true

iv0 - June 30, 2009 - 08:13

Just tried it and I can confirm, that the way described above works also when a menu item does not expand as expected. This usually happens when you have a multi language website, with all menu items for individual language homepage pointing to <front>. This leads to a problem, when the menu expands only on one of the language homepages, on the other language versions the homepage menu items do not expand.

Then you need to make two menu items for such homepage:
1. The menu item created later (has a higher id in the database) must be the visible parent (that shall be expanded).
2. The menu item created earlier (has a lower id in the database) must be the hidden child (that makes the parent expand).
Both menu items have to point to respective node/{id} (i.e. node/1 in Czech and node/15 in English), not to <front>, in all languages!

This brings up a new problem of having duplicate content for homepage on more than one URL, which I decided to solve using Global Redirect module.

So it obviously has something to do with database order of menu items. Thank you so much for a suggestion, I have been fighting with this for ages.

 
 

Drupal is a registered trademark of Dries Buytaert.