3

I can't get the wildcard from a hook_menu to pass it to a callback function

Here is what I have:

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

    $items['spin/login/%'] = array(
        'title' => 'SPIN',
        'type' => MENU_CALLBACK,
        'access callback' => TRUE,
        'page callback' => 'spin_check_access'
    );

    return $items;
}

function spin_check_access($type = '') {
    echo $type; // type is never the %
}

Is it correct? I need the $type to be replaced with the wildcard's value.

2

You have two options:

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

  $items['spin/login'] = array(
    'title' => 'SPIN',
    'type' => MENU_CALLBACK,
    'access callback' => TRUE,
    'page callback' => 'spin_check_access',
  );

  $items['spin/login/%'] = array(
    'title' => 'SPIN',
    'type' => MENU_CALLBACK,
    'access callback' => TRUE,
    'page callback' => 'spin_check_access',
    'page arguments' => array(2),
  );

  return $items;
}

Hook menu will not send any wildcard params to the callback function, but it will send params not defined in the url. So in the first example if you go to spin/login type will be '' and spin/login/foo type will be foo. For the second example spin/login will return 404 and for spin/login/foo type will be foo.

1

You are missing page arguments, telling Drupal which values to pass on. Try:

$items['spin/login/%'] = array(
    'title' => 'SPIN',
    'type' => MENU_CALLBACK,
    'access callback' => TRUE,
    'page arguments' => array(2),
    'page callback' => 'spin_check_access'
);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.