[support] tutorial on dynamically generating menu in D5

Metzler, David metzlerd at evergreen.edu
Tue Jul 29 15:47:05 UTC 2008


>did you mean swap($may_cache,!$may_cache)?

No I don't think so, but maybe I didn't word that sentence well enough.

>I didn't notice the (!may_cache) section since what I'd expect to be a
$items[] = array('path' => 'user' ...
>is actually written as
>if ($_GET['q'] == 'user'
>Why?

That chunk of code is really wrapping a a redirect.  It's saying if
you're on the 'user' page (not subpages) and we're logged in then goto
user/{uid}.   The code that builds the $items array is a few lines down
and incidently is not hit if you just goto yousite/user.  If you visit
the yoursite/user page and watch the url line you'll see the redirect
that's happening.  But this isn't really part of building menus at all. 

>The "My account" hook is defined as MENU_DYNAMIC_ITEM in the $may_cache
section... Does it works as expected because 
>once a user is logged in cache can't be used?

No that's not true. Cache can be used whether or not a user is logged
in, but the $items array in the if (!$maycache) section may register new
menu paths that weren't available when you're logged in. Menu items are
cached for anonymous users as well. But MENU_DYNAMIC_ITEMS are never
cached. 

Note that the tabs for that page are rendered later, in the if
(!may_cache) section

>Do you mean "I can't put it into a block with the admin interface"?
>If I had a
>path=>'root' in $may_cache
>and a
>path=>'root/otherstuff' in !$may_cache
>do you mean I won't see the later?
>Wouldn't it be enough to just define
>path=>'root/otherstuff'
>as MENU_DYNAMIC_ITEM
>and place it in the $may_cache section?

I really just meant that you can't manipulate root/otherstuff in the
admin interface at all. I'm not really familiar with MENU_DYNAMIC_ITEM.
I think it's historical.   The same kind of dynamic menu items appear in
the if (!may_cache ).  Either way your menu items can't be altered in
the admin interface. 

>What does it mean I can't have code defining a new menu?
>I can actually define conditionally a menu tree even in the $may_cache
section.
>I didn't see user.module calling menu_rebuild but eg. taxonomy_menu
rebuild the menu when the taxonomy hook is fired.

I mean that you can't define something that is in its own block by
default.  All menus in code start out in the "Navigation Menu". 

>When does the menu_rebuild have to be called if I have dynamic menu?

It doesn't because items in this sections are built at every page load,
and not stored in the database at all. The menu_rebuild functioin is
simply a cache clearing mecahnism.  

>I won't just have to deal with menu appearing and disappearing
according to access but also on other conditions.
>eg. if I'd like to dynamically create a menu "See this list" just when
the user (even anonymous user) has some item in
>the list?

You may find DYNAMIC_MENU_ITEM to be enough, but I have never used it,
so can't attest to the differences.  I really do think it's an older way
of achieving the same thing. 

>What if I want to change at which depth I want to start to render a
menu in a block?
As you hinted at in your root example, the starting point simply needs
to be a static menu (meaning that it is in the if ($may_cache) section
of hook menu, and hence movable by the admin interface. 
   

>Then what is the difference between defining a menu DYNAMIC or placing
it in the !$may_cache section?
I think MENU_DYNAMIC_ITEM  is a hold-over from the past, but I'm not
sure.  As I've said, I didn't use it much and I've always been told to
put dynamic menus in the if (!may_cache) section. 


>Still didn't get it.
>As you said... if at least a parent is in the $may_cache system I could
use the admin interface to add a block that 
>contain a hierarchy even if some sub menu may be dynamic or in the
!$may_cache section. Is it?

I think that this is doable, yes. 

>What if all my menu is dynamic? How can I place a menu in a block?

You can't.  If all your menu is dynamic you can't place it in another
block. 

>Still reading the api... but it seems that this part of Drupal lack
tutorials, examples or books that explain how to use >it.
>I checked twice "Pro Drupal Dev" and I didn't find anything interesting
about dynamic menu, there is nothing even in the >cache section.
>I gave a quick look to "Learning D6 module development" but it doesn't
seem to contain any useful info on this topic.

In D6 the menu system was completely overhauled and is very different
from D5.  In those you simply write a and access function and have it
get called.  IF the access function returns true, then you're menu item
gets displayed.  Don't think of the access function as simply being
about permissions. In d6, dynamic paths are achieved with wild_cards.
You register user/%/edit to get the user edit page where the uid passed
as a parameter.  

In D5 this approach will not work because the user_access function is
not called on every page_load.  It's results get cahced at login, so you
have to use the if (!may_cache) section in order to achieve truly
dynamic menus. 

thanks

--
Ivan Sergio Borgonovo
http://www.webthatworks.it

--
[ Drupal support list | http://lists.drupal.org/ ]


More information about the support mailing list