[support] tutorial on dynamically generating menu in D5

Metzler, David metzlerd at evergreen.edu
Mon Jul 28 17:16:37 UTC 2008


The reason that these menus is because a.) they appear in the if
(!$may_cache) section of the hook_menu and b.) thay are wrapped in an if
($user !== FALSE) condition.  

Note that there is also a separate /user path registered in the if
($may_cache) section of the hook_menu. This is the "default" path for
/user and what shows up in the admin/menu screen. 

You may be grappling with one of the core troubles with Drupal 5.  Only
menus that are in the if ($may_cache) section of the module may be moved
around with the admin/menu user interface.  If it's a dynamic menu, you
just don't see it there.  So if you make a dynamic menu item, you can't
turn around and move it into a block.  Although you may be able to move
a the static parent menu item (defined in the if ($may_cache) section of
the menu into a separate menu and menu block. This took me a little time
to grok. 

Finally you can't have the code define a new menu, but only create a
menu tree section in code and then move it later using the admin menu
interface.  

Is that clearing any of this up for you? 


Here's the relavent chunk of user_menu code for understanding. 
  if ($may_cache) {
    $items[] = array('path' => 'user', 'title' => t('User account'),
      'callback' => 'drupal_get_form', 'callback arguments' =>
array('user_login'),
      'access' => !$user->uid, 'type' => MENU_CALLBACK);

    $items[] = array('path' => 'user/autocomplete', 'title' => t('User
autocomplete'),
      'callback' => 'user_autocomplete', 'access' => $view_access,
'type' => MENU_CALLBACK);
    ... Bunch of code ommitted.... 
    
  } else { 
    ... Some code ommitted.
      if ($user !== FALSE) {
        // Always let a user view their own account
        $view_access |= $user->uid == arg(1);
        // Only admins can view blocked accounts
        $view_access &= $account->status || $admin_access;

        $items[] = array('path' => 'user/'. arg(1), 'title' =>
t('User'),
          'type' => MENU_CALLBACK, 'callback' => 'user_view',
          'callback arguments' => array(arg(1)), 'access' =>
$view_access);

        $items[] = array('path' => 'user/'. arg(1) .'/view', 'title' =>
t('View'),
          'access' => $view_access, 'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => -10);

        $items[] = array('path' => 'user/'. arg(1) .'/edit', 'title' =>
t('Edit'),
          'callback' => 'drupal_get_form', 'callback arguments' =>
array('user_edit'),
          'access' => $admin_access || $user->uid == arg(1), 'type' =>
MENU_LOCAL_TASK);
        $items[] = array('path' => 'user/'. arg(1) .'/delete', 'title'
=> t('Delete'),
          'callback' => 'user_edit', 'access' => $admin_access,
          'type' => MENU_CALLBACK);

        if (arg(2) == 'edit') {
          if (($categories = _user_categories($account)) &&
(count($categories) > 1)) {
            foreach ($categories as $key => $category) {
              $items[] = array(
                'path' => 'user/'. arg(1) .'/edit/'. $category['name'],
                'title' => $category['title'],
                'type' => $category['name'] == 'account' ?
MENU_DEFAULT_LOCAL_TASK : MENU_LOCAL_TASK,
                'weight' => $category['weight'],
                'access' => ($admin_access || $user->uid == arg(1)));
            }
          }
        }
      }
    }
-----Original Message-----
From: support-bounces at drupal.org [mailto:support-bounces at drupal.org] On
Behalf Of Ivan Sergio Borgonovo
Sent: Monday, July 28, 2008 8:43 AM
To: support at drupal.org
Subject: Re: [support] tutorial on dynamically generating menu in D5

On Mon, 28 Jul 2008 16:07:50 +0200
Florent JOUSSEAUME <florent.jousseaume at makina-corpus.com> wrote:

> For your example, this is not a switch, but 2 modules with differents 
> ACL. The block 'login' is displayed for 'Anonymous' and the menu is 
> displayed for 'Authenticated user' (block Navigation).
> 
> For the element's visibility in the menu, this is with the user_access

> method defined in the function 'hook_menu' for each module.

It looks a bit hakish.
And I still can't get it completely.

1)
path user points to user_login if(uid) otherwise it should point to
something else... but what is the alternative?
In user_menu all paths are user/[something else] or admin/user/...
I can't understand why once the user is logged in and the path
user/[uid] becomes available the path /user reaches user_view

2)
I can't yet understand the magic that make -My account appear in the
Menu admin pages as locked. I still haven't had the time to read the
code.

3)
I miss how I can *render* menu with the admin interface and/or in
modules.
I'd expect that if I build up a $items hierarchy I could build up a menu
from the "admin" interface adding a path and the menu system will take
care of rendering children once I eg. put the menu in a block.
What if I'd like to render the menu inside my code? I think I should use
menu_tree... but well I bet there are a lot of tricks I could learn
without guessing them from the API.

4) I haven't seen any tutorial, handbook... on any of the menu_ family
of function.
There is nothing on "Pro Drupal development" book.

thanks

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

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


More information about the support mailing list