<?php
/*

The following code does not do what I need or expect it to do. This is the first module I have attempted.
The plumbing is correct , as it does work - sort of.

What I am trying to do is add a button to the menu that will take the user to a custom page constructed from their
username. (construction of that page is fine, that is not the issue)

The code below works, but only for the lone administrator user.
The button does not show up for any other users. This appears to be because each user's page is set up as private
so permissions do not pass?. But if I force the permission to TRUE the button shows up, but the path it tries to access is
always for the administrator user. There is nothing dynamic about this approach. Why doesn't it work???

So, how can I accompish this? Obviously I am missing something, either in syntax/parameters or in my understanding of how the
drupal environment works.

It seems like this general approach simply will not work at all.
*/
/* drupal 6.x module */

/**
* Implementation of hook_menu().
*/
function myfavorites_menu() {
global $user;

$items = array();

$items[$user->name] = array(
'title' => 'My Favorites',
'description' => 'Go to Your Favorites Page',
'page callback' => 'go_home', /* same result if I cut out the middle man and use drupal_goto here */
'page arguments' => $user->name,
'access callback' => 'allow_all', /* should be 'access_user' but changed so it shows up all the time (I think this could be simply TRUE
without the function invocation)
This is weird because it seems like it is being
executed , as it prohibits access/display of menu item
to user other than the administrator */
/* I am disturbed by the passing remark in the documentation
"This hook is called rarely - for example when modules are enabled."
However, It must work somehow, as Others seem to have reported doing
dynamic menus in a similar way with previous drupal versions.
If I purposefully put a bad line in the code I get an error on any access so
it seems like it is being executed on every page access , yet the
"target" link is unchanged ??? */
'access arguments' => array($user->name),

/* Doesnt help 'type' => MENU_CALLBACK|MENU_NORMAL_ITEM, */

);

return $items;
}

function go_home($path) {
drupal_goto($path);
}

function allow_all($name) { /* this is bogus to force permission to be granted in all cases */
return TRUE;
}

I'm at jbonsteel@gmail.com

Thanks .

Comments

gforce301’s picture

However, It must work somehow, as Others seem to have reported doing dynamic menus in a similar way with previous drupal versions.

The D6 menu has the ability to create dynamic menu items, it just does it differently than previous versions of drupal.

Start reading here --> http://drupal.org/node/109131
This page deals specifically with what you need I think --> http://drupal.org/node/109153

jbonsteel’s picture

From the page you cited. I see.
"

  $items['node/%node'] = array(
    'access callback' => 'node_access',
    'access arguments' => array('view', 1),
    'page callback' => 'node_view_page',
    'page arguments' => array(1),
  );

"
It seems like this or something close is what I need to adapt. The doc refers to argument 1. Argument 1 of what function? I do not know if the code above is sort of pseudo code or actual code. Here is my last attempt at this.(below) It does not work at all. No errors but no menu button in any case. I've tried a whole bunch of other variations, but it does not make sense to me. I have been an assembly language programmer for over 20 years. But I am new to php and drupal. I guess I need help deciphering this documentation. It seems cryptic and obtuse to me. It is making some assumptions I am not in on yet.

/*

/* drupal 6.x module */

/**
 * Implementation of hook_menu().
 */
function myfavorites_menu() {
 global $user;

  $items = array()
;
           ?????????
                 V ?
  $items['node/%'] = array(
    'title' => 'My Favorites',
    'description' => 'Go to Your Favorites Page',
    'page callback' => 'node_view_page',       /* same result if I   use drupal_goto here */
                                          ???????????
                                                V ?
    'page arguments' => array($user->name),
    'access callback' => 'allow_all', /* should be 'access_user'or 'access_content' 
                                         but changed so it shows up all the time (I think this could be simply TRUE
                                           without the function invocation)     ??? */
    'access arguments' => array($user->name),
    
    
  );

  return $items;
}
 

function allow_all($name) { /* this is bogus to force permission to be granted in all cases */
return TRUE;
}

( added so it shows up prettier )
What is the correct syntax here?
Thanks.
Jeff

mradcliffe’s picture

This is an easier way of grabbing drupal url arguments. Take for example this path: node/5/edit. 5 would be
argument 1. This will pass in the $node object for that path (since it's a node, you can do the same for users). It's still kind of confusing I guess. It's so you don't have to mess with if (is_numeric(arg(1))) crap.

Here's another example. If this was complete it'd pop a local task or tab on every node if you had 'some permission here' access:

   $items['node/%/mytab'] = array(
       'title' => 'My tab',
       'type' => MENU_LOCAL_TASK,
       'page callback' => '_my_callback',
       'page arguments' => array(1),  // _my_callback($node)
       'access callback' => 'user_access',  // We don't need this if we're using user_access, but if we wanted to have our own access callback like _my_access_callback($node) we could
       'access arguments' => array('some permission here') // see admin/user/permissions
   );

In your example above that you tried:

$items['node/%'] = array(   /* this is a bad path, i'd use user/%/favorites, but that's me.   */
    'title' => 'My Favorites', /* good */
    'description' => 'Go to Your Favorites Page', /* not necessary */
    'page callback' => 'node_view_page',       /* will call node_view_page, though I dunno why you would want to use this for favorites?? Make this your own custom callback in your module like _mymodulename_favorites_page */
    'page arguments' => array($user->name), /* if you're using wildcards in your path, put the argument number as one of the array members, which is why I suggest user/%/favorites so you can just do array(1) and you get a $user object for _mymodulename_favorites callabck */
    'access callback' => 'allow_all', /* if you're going to make this user_access you don't need it. I don't think user_access() accepts a username as a permission, but I could be wrong. You may need a custom callback here like _mymodulename_favorites_access */
    'access arguments' => array($user->name), /* probably pass in array(1), and then just grab the $user->name object in your custom access callback. Not sure what the $user object is when menus are rebuilt, but this is probably not what you want. */
//    'type' => MENU_LOCAL_TASK /* you probably need a type, i'd either use MENU_LOCAL_TASK or MENU_NORMAL_ITEM, MENU_CALLBACK is also an option, but it's probably not what you are looking for ui-wise */
  );