I have a piece of code lifted out from Pro Drupal Development 2E, which originally came from user module.

$items['user'/%user_uid_optional'] = 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),
  'file' => 'user_pages.inc',
);

I could not figure out what all the various array(1) in the code is for. Could anyone enlighten me on this?

Comments

mikeker’s picture

The array( 1 ) bits refer back to the URL in question. It's a zero-indexed reference to the parts of the URL in $items. In this case, array( 1 ) refers to the second part of the URL or %user_uid_optional.

I don't think I'm making much sense... Perhaps an example?

For example going to http://example.com/node/zero/one/two/three with a hook_menu including

$items['node/%/%/%/%'] = array(
  'title' => 'Example',
  'page callback' => 'SomeExampleFunction',
  'page arguments' => array( 0, 1, 2, 3 ),
);

function SomeExampleFunction( $arg0, $arg1, $arg2, $arg3 ) {
  print "arugments are: $arg0, $arg1, $arg2, $arg3";
}

Would result in

arguments are: zero, one, two, three

I agree, it's not well documented. And judging my how poorly I just explained it, perhaps I can see why!

Hope that helps.

Mike Keran
www.MikeKeran.com

- Mike

Noktha’s picture

Thank you for your post. However, I would like to point out just a slight mistake.

After running the code above, the output should be

arguments are: node, zero, one, two

instead of

arguments are: zero, one, two, three

since we have set
'page arguments' => array( 0, 1, 2, 3 ) in the example above.

I see parallels of this with command line arguments in Linux C programming (an example can be seen at http://www.physics.drexel.edu/students/courses/Comp_Phys/General/C_basic...), with the executable name replaced with the module name and using "/" as delimiters instead of the normal space in Linux. This small trivia may be useful for those of us with C programming background trying to work with Drupal.

Further more, it *is* possible to do funky stuff like 'page arguments' => array(1,2,1,2) and end up with

arguments are: zero, one, zero, one

but we just need to remember to clear the cache before testing it out.

mikeker’s picture

You're absolutely correct. That'll teach me to code in a comment!

Thanks for the correction -- I'd hate to leave misinformation for others to trip over.

Mike Keran
www.MikeKeran.com

- Mike