API page: http://api.drupal.org/api/drupal/modules--system--system.api.php/functio...

Describe the problem you have found:

If you pass an integer in 'access arguments' (and the other callbacks), it is interpreted as an index into the path. If you want to pass an actual integer to the function, you need to make it be a string, such as '2' instead of the integer 2.

This is not documented in hook_menu() in the section that talks about path substitution, and it should be.

davereid points out on IRC that it *is* documented here: http://drupal.org/node/109153#integers

So this should be adapted and put into the hook_menu doc for d8/7 and maybe even d6. Might be a good project for a Novice doc contributor?

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Dave Reid’s picture

I think we just need to add a small clarification to this part:

In addition to optional path arguments, the page callback and other callback functions may specify argument lists as arrays. These argument lists may contain both fixed/hard-coded argument values and integers that correspond to path components. When integers are used and the callback function is called, the corresponding path components will be substituted for the integers. That is, the integer 0 in an argument list will be replaced with the first path component, integer 1 with the second, and so on (path components are numbered starting from zero). This substitution feature allows you to re-use a callback function for several different paths.

Proposed:

In addition to optional path arguments, the page callback and other callback functions may specify argument lists as arrays. These argument lists may contain both fixed/hard-coded argument values and integers that correspond to path components. When integers are used and the callback function is called, the corresponding path components will be substituted for the integers. That is, the integer 0 in an argument list will be replaced with the first path component, integer 1 with the second, and so on (path components are numbered starting from zero). This substitution feature allows you to re-use a callback function for several different paths. To pass an integer without it being replace with its respective path component, use the string value of the integer (e.g. '1') as the argument value.

barbi’s picture

subscribe

jhodgdon’s picture

Looks pretty good. Slight changes to the added text from #1:

To pass an integer without it being replaced with its respective path component, use the string value of the integer (e.g., '1') as the argument value.

[replace -> replaced, and comma after e.g.]

barbi’s picture

Assigned: Unassigned » barbi
Status: Active » Needs review
FileSize
903 bytes
jhodgdon’s picture

Status: Needs review » Reviewed & tested by the community

Thanks! D7/8 please... then we should probably mark "patch/to be ported" for D6.

webchick’s picture

Status: Reviewed & tested by the community » Needs review

Hm. The text reads to me as though there's about to follow an example of passing in an argument as a string, but that's not what follows:

  function mymodule_menu() {
    $items['abc/def'] = array(
      'page callback' => 'mymodule_abc_view',
      'page arguments' => array(1, 'foo'),
    );
    return $items;
  }

Should we expand the example to also include a literal '1'? Or slightly re-write the text so that the association is not so close?

jhodgdon’s picture

Status: Needs review » Needs work

Agreed. How about if we move the additional sentence up by one sentence? So it would read:

That is, the integer 0 in an argument list will be replaced with the first path component, integer 1 with the second, and so on (path components are numbered starting from zero). To pass an integer without it being replaced with its respective path component, use the string value of the integer (e.g., '1') as the argument value. This substitution feature allows you to re-use a callback function for several different paths. For example: [example follows]

barbi’s picture

Status: Needs work » Needs review
FileSize
1.04 KB

Rearranged the sentence order and recreated the patch.

joachim’s picture

Status: Needs review » Reviewed & tested by the community

Looks good to me.

jhodgdon’s picture

me too.

This is for 8.x/7.x by the way. Then we should probably mark "patch/to be ported" for D6.

webchick’s picture

Status: Reviewed & tested by the community » Fixed

Perfect!

Committed to 8.x and 7.x. Thanks!

webchick’s picture

Version: 8.x-dev » 6.x-dev
Status: Fixed » Patch (to be ported)
Issue tags: -Needs backport to D7

Oops.

drupal_was_my_past’s picture

Status: Patch (to be ported) » Fixed
Issue tags: -Needs backport to D6

Looks like D6 for this issue was fixed in #617424: Error in hook_menu documentation for load arguments.

Automatically closed -- issue fixed for 2 weeks with no activity.