1

I'm writing a Drupal 6 module that sends a user ID + a randomly generated string to a function through the URI. I'm using the menu hook:

function invites_menu() {    
  // ...    
  $items['invites/auth'] = array(
    'title' => 'Are you human?',
    'page callback' => 'invites_pageAuth',
    'access arguments' => array('access invites content'),
    'page arguments' => array(2),
    'type' => MENU_CALLBACK
  );
  // ...
}

I am new to Drupal, but as I understand it (and I could well be mistaken) this should pass two values to the callback function, which for testing purposes currently looks like this:

function invites_pageAuth($auth = NULL, $uid = NULL) {
  drupal_set_message("uid: $uid <br /> $auth");
}

The URL I use is 'invites/auth/RANDOMSTRING/USERID'. This seems to get the first value twice; both $auth and $uid contain 'RANDOMSTRING'.

Am I missing something really simple?
Thank you.

apaderno
  • 28,547
  • 16
  • 75
  • 90
Aaron
  • 458
  • 4
  • 16

2 Answers2

4
function invites_menu()
{    
...    
    $items['invites/auth/%/%'] = array(
            'title' => 'Are you human?',
            'page callback' => 'invites_pageAuth',
            'access arguments' => array('access invites content'),
            'page arguments' => array(2, 3),
            'type' => MENU_CALLBACK
          );
    ...
}

Also you can use arg(3) as user object -> use %user instead % and you will get user object in your callback

$items['invites/auth/%/%user']
dobeerman
  • 1,354
  • 15
  • 27
  • Thanks! it was as simple as adding this, and importantly (for drupal noobs like me), clear the cache in admin > config > site performance. I had caching disabled in these settings but clearing it still made a difference. – Aaron Mar 19 '11 at 10:14
  • You might even want to add two explicit arguments (invites/auth/%/%) and extend page arguments to array(2,3). Right now, someone could all that URL without a uid, doing that would prevent that. Also, the cache settings in the UI are just for the page cache. Everything else is still cached and the menu system isn't even a cache at all. – Berdir Mar 19 '11 at 12:04
1

My reading of the docs is that 'page_arguments' => array(2) means: "pass path component number 2 as the first argument to the page callback, followed by any optional arguments". So you'd get: RANDOMSTRING, RANDOMSTRING, USERID.

Warning: I am not a Drupal expert and the above may be wrong. You can check it easily enough by giving invites_pageAuth another argument, changing the 2 in your page_arguments, etc.

Gareth McCaughan
  • 19,888
  • 1
  • 41
  • 62