passing integers as parameter in drupal menu
-
24-06-2021 - |
Pergunta
I'm using drupal 6
Suppose I have
$items['path/yo'] = array(
'page callback' => 'callback_function',
'page arguments' => array(1),
'type' => MENU_CALLBACK,
);
It will instead pass in the part of the path that is in the 1-th position (in this case it will pass 'yo') into the callback_function function...
But what if I'm actually TRYING to pass in the integer 1 into the function? How would I do that without casting it as string first and then reconverting to integer...
Solução
$items['path/yo'] = array(
'page callback' => 'callback_function',
'page arguments' => array("1"),
'type' => MENU_CALLBACK,
);
See Wolfe's answer as well. If you enter (int) 1, it will be arg(1). Enter strings to be passed to the page callback as-is.
Outras dicas
php type conversion is very well done, you have to be careful with type casting however especially with 0 and 1 as they can be strings, numbers or booleans.
You can use type checks in your conditionals (such as === !==). In your current example it isn't a string first it's a number. It would pass 1 into callback_function.
function callback_function($args) {
print_r($args);
}
Will give you the arguments passed. In this case $args[0]
would be the number 1. You shouldn't have to worry if it's a number or a string in 99% of cases because if you use it as a number php will convert it to a number and if you use it as a string php will treat it as a string. Just be careful with conditional statements and be sure to read this: http://php.net/manual/en/language.operators.comparison.php
For example to see if it is the number 1:
if(1 === $args[0]) echo "Numbah one!";
Will only print "Numbah one!" if it's a number type and the number 1. You can typecast it if you like with
(int)$args[0];
(string)$args[0];
(boolean)$args[0];
respectively.
You might also check out this article: http://drupal.org/node/1473458