By dakku on
Hi,
I am writing a module that implements the following URL structure:
domain.foo/0123456
as you can see, arg(0) in this case is numeric and under drupal 6 integer values pass the corresponding URL component.
I am trying to do:
function foobar_menu(){
$items['%foobar_id'] = array(
'page callback' => 'foobar',
'access arguments' => array('access content'),
'page arguments' => array(0),
'type' => MENU_CALLBACK,
);
}
function foobar_id_load($foobar_id){
return $foobar_id;
}
Currently this is returning 404, it appears drupal doesnt like arg(0) to be numeric, any way to implement this?
Cheers
d
Comments
hmm, After a quick chat with
hmm,
After a quick chat with Rob Douglas, my first impression is that drupal 6 is not setup to be able to handle wildcards at arg(0)
Can anyone please shed some light on this?
It might be this won't work,
It might be this won't work, because every menu path will match the wildcard. I'm not sure as I haven't studied the whole wildcard thing yet, but it might be worth it to try a path like foobar/%foobar_id.
yes if I do
yes if I do
domain.foo/foobar/%foobar
that results in a page, but this solution is not SEO friendly as such as %foobar will be 1 level deeper from the homepage according to google.
Alternative solution for now is:
domain.foo/blah-0123456
This kinda makes me wanna go back to drupal 5
Thoughts...
I've run a test and come up with the following hypothesis...
Consider the following paths (which would be used as an index of the
$itemsarray):'1234''test/1234'Now look at the menu_router_build function...
Note in that in that function, it loops over all hook_menu functions and builds and array of paths => modules as
$callbacks. After each module has thrown back its$itemsarray, the results arearray_merge'd into$callbacks.Now take a quick look at the
array_mergepage and you'll learn thatarray_mergewill reset any numeric indexes.So? Why does this matter to us? We defined our
$itemsarray keys as STRINGS (hence the single quotes around them). PHP is a kind language most of the time - but in this case its being evil. It's "efficiently" casting our string 'numbers' into ACTUAL numbers.Here is a summary of what happens:
array_mergeand PHP kindly resets all numeric indexes for us.This is why our lovely '1234' never appears as a callback.
Now part of the solution is to change that:
to this:
or maybe...
... if you like short lines.
I tried this and did a
print_rafter the $callbacks was built - the '1234' entry was PRESERVED (albeit as an integer, not string). However the menu callback still did not function. I assume there is some morearray_mergemalarkey going on somewhere else which is probably causing the same problem (in which case we've simply moved the problem from one cog in the machine to another).Thoughts?
Thoughts? I think this is a
I think this is a good reason for a bug report for 6.x-dev. For now you should change the paths.
By the way: Kudos for the research, Nicholas!
One addition...
Consider:
$items['%/test'] = ...<./code>Why doesn't this work?
Does PHP 'like' array keys starting with symbols? I'm pretty sure it doesn't like VARIABLE names starting with numbers and symbols...
return $items
Could it be as simple as that you forgot to return $items at the end of hook_menu?
i am also having same
i am also having same problem but it my case arg(0) is a string still my function doesn't get called
and here arg(0) which i get is a string like 'tagterm'.
if problem is obly with integer numbers, it should work in my case ???
Bug added
Hi,
This bug is _really_ bugging me on Drigg.
I reported it:
http://drupal.org/node/314406
Bye,
Merc.