Tried to find some documentation on this that includes examples I can use, but couldn't. If anyone has suggestions, please let me know.

According to http://drupal.org/node/103114, I assume that everything that went into D5 hook_menu $may_cache stays in hook_menu() (with new syntax). It also appears that everything that was !$may_cache goes into hook_init(). But, I don't see the syntax for that.

So, for example, in 5.x my hook_menu() is:

  $items = array();

  if ($may_cache) {
    $items[] = array(
      'path' => 'admin/og/og_user_roles',
      'title' => t('Organic groups user roles'),
      'description' => t('Allows group administrators to add members into group roles.'),
      'callback' => 'drupal_get_form',
      'callback arguments' => 'og_user_roles_admin_settings',
      'access' => user_access('administer og_user_roles'),
      'type' => MENU_NORMAL_ITEM
    );
  }
  else {
    // modr8 modification
    if (module_exists('modr8')) {
      if (arg(0) == 'node' && is_numeric(arg(1))) {
        $node = node_load(arg(1));
        if (!empty($node) && $node->moderate) {
          $items[] = array('path' => 'node/'. $node->nid .'/ogmodr8', 'title' => t('Moderate'),
            'callback' => og_user_roles_modr8_page,
            'callback arguments' => $node->nid,
            'access' => user_access('moderate content'),
            'weight' => 5,
            'type' => MENU_LOCAL_TASK);
        }
      }
    }
    // Add another tab to the group subscribers page for admins to
    // configure member roles
    if (arg(0) == 'og' && arg(1) == 'users' && is_numeric(arg(2))) {
      $gid = arg(2);
      if (og_user_roles_is_allowed($gid)) {
        $items[] = array(
          'path' => "og/users/$gid/roles",
          'title' => t('Configure member roles'),
          'callback' => 'og_user_roles_page',
          'callback arguments' => array($gid),
          'access' => user_access('configure member roles'),
          'weight' => 5,
          'type' => MENU_LOCAL_TASK
        );
      }
    }
  }

  return $items;

How do I code everything in the "else" clause in hook_init() (if that is what I'm supposed to do?)?

Thanks for any help anyone can provide.

Comments

TapocoL’s picture

The may_cache variable is not used anymore for hook_menu. It looks like you are interested in wildcards:
http://drupal.org/node/109134

Also, here is an example on the API Site:
http://api.drupal.org/api/function/page_example_menu/6

-Craig Jackson
-Web Developer

somebodysysop’s picture

It's slowly starting to make sense. This example actually helps. Guess I don't need hook_init after all!

somebodysysop’s picture

I'm beginning to modify a module that can't yet be activated in D6 because the module it depends on (Organic Groups) doesn't yet have a version 6 release. I've read the new hook_menu documentation, but still come upon some things that I'm just not quite sure about.

Is this how I convert the following in hook_menu?:

    # Drupal 5.x
    $items[] = array(
      'path' => 'oguseredit/' . arg(1),
      'title' => t('Edit user'),
      'callback' => 'og_user_roles_oguseredit',
      'callback arguments' => array(arg(1)),
      'access' => $user->uid,
      'type' => MENU_CALLBACK
    );

To this?:

    # Drupal 6.x
    $items['oguseredit/%'] = array(
      'title' => 'Edit user',
      'page callback' => 'og_user_roles_oguseredit',
      'page arguments' => array(1),
      'access callback' => 'user_is_logged_in',
      'type' => MENU_CALLBACK
    );

I thought about using 'oguseredit/%user', but user_load requires the argument array(uid=>number), and I don't see instructions for that here.

TapocoL’s picture

If the oguseredit/% is editing your own account, you can use user_current after it. The user module does it like this:

  $items['user/%user_current'] = array(
    'title' => 'My account',
    'title callback' => 'user_page_title',
    'title arguments' => array(1),
    'page callback' => 'user_view',
    'page arguments' => array(1),
    'access callback' => 'user_view_access',
    'access arguments' => array(1),
    'parent' => '',
    'file' => 'user.pages.inc',
  );

That creates the 'My Account' menu item and it will link to the current user's profile. Plus %user_current also loads the current user object when passing the argument.

-Craig Jackson
-Web Developer

somebodysysop’s picture

  $items['node/agenda/list/%node'] = array(
    'title callback' => 'node_page_title',
    'title arguments' => array(3),
    'page callback' => 'agenda_list',
    'page arguments' => array(3)->nid,
    'access callback' => 'user_access',
    'access arguments' => array('view agenda'),
    'type' => MENU_CALLBACK
  );

Since array(3) represents node_load(arg(3)), can I use the statement array(3)->nid for the node id. Or, for that matter, array(3)->type for the node type?

TapocoL’s picture

I would not think so, because the array(#) is not an object (EDIT: Let me rephrase. When you pass the array. It does not know what the method nid is to the class array. I might be making it more confusing.). It is during the menu building process, that creates the object, and passes it as arguments to the callback function. Well, I have just passed the whole node, unless you are using a lot of memory, it shouldn't be that big of a difference for you. However, if memory is a problem and you only want the nid as a variable and not the node object. You could just use the page callback as a means of passing only the nid.

function agenda_menu() {
  $items['node/agenda/list/%node'] = array(
    'title callback' => 'node_page_title',
    'title arguments' => array(3),
    'page callback' => 'agenda_list',
    'page arguments' => array(3)->nid,
    'access callback' => 'user_access',
    'access arguments' => array('view agenda'),
    'type' => MENU_CALLBACK
  );
}

function agenda_list($node) {
  _agenda_list($node->nid);
}

function _agenda_list($nid) {
  echo 'blah blah blah';
}

-Craig Jackson
-Web Developer

somebodysysop’s picture

Looks like I just needed to re-think what I was doing:

function agenda_menu() {
  $items = array();

  $items['node/agenda/list/%node'] = array(
    'title callback' => 'agenda_page_title',
    'title arguments' => array(3, 'List agenda items for: '),
    'page callback' => '_agenda_list',
    'page arguments' => array(3),
    'access callback' => 'user_access',
    'access arguments' => array('view agenda'),
    'type' => MENU_CALLBACK
  );

  return $items;
}

function _agenda_list($node) {
  return agenda_list($node->nid);
}

function agenda_page_title($node, $text) {
  $return = $text . $node->title;
  return $return;
}

Thanks so much for the input!