0

I have this two test menu items:

  $items['test/%id'] = array(
    'title' => 'Test',
    'type' => MENU_LOCAL_TASK,
    'page callback' => 'drupal_get_form',
    'page arguments' => array('my_module_form', 1),
    'load arguments' => array('%map'),
    'access arguments' => array(1),
  );

  $items['test/new/%new_id'] = array(
    'title' => 'Test',
    'type' => MENU_LOCAL_TASK,
    'page callback' => 'drupal_get_form',
    'page arguments' => array('my_module_form', 2),
    'load arguments' => array('%map'),
    'access arguments' => array(2),
  );

Here is the fr_module_form function:

function my_module_form($form, &$form_state, $id = NULL, $new_id = NULL) {
  dsm($id, 'id');
  dsm($new_id, 'new_id');
  return '';
}

Here is the problem. When I visit (for instance): test/my-test-var
dsm(1) function returns $id => my-test-var
dsm(2) function returns $new_id =>

but when I visit: test/new/my-test-var
dsm(1) function returns the same as above which is:
$id => my-test-var so it's not $new_id as I've expected.
dsm(2) is still empty

Moreover if I visit /test/new/my-test-var/my-second-test-var I get:
dsm(1): $id => my-test-var
dsm(2): $new_id => my-second-test-var

Why do I 'tell' drupal in 'page arguments' which argument should be used? I always get the first argument from my_module_form.

How to separate it?

1

Path parts are passed to the menu callback function with a zero index as the first path part i.e.

First url: array(0 => 'test', 1 => '%id')

Second url: array(0 => 'test', 1 => 'new', 2 => '%new_id')

Your form function expects two arguments but you only ever give it one.

In your form function arguments, this: $id = NULL, $new_id = NULL corresponds to this array('my_module_form', first_argument, second_argument) in your menu callback args. So you are only ever giving your form function one argument from the menu. Try this in your second menu item:

'page arguments' => array('my_module_form', 1, 2),

This will provide two arguments for the form callback function. Your first menu item will return null for $new_id, because you don't have further path parts to provide from the url.

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.