Hi, does anyone have any ideas why using a wildcard for a menu callback would always be empty, even when the page definately exists?
For example, say i have the following callback configured (for testing purposes):
$items ['content/%content/test] = Array (
'title' => 'Test',
'type' => MENU_CALLBACK,
'access callback' => 'user_access',
'access arguments' => array('view'),
'page callback' => 'testContent',
'page arguments' => Array (1)
);
And i am receiving this here:
function testContent($content)
{
print_r($content);
exit;
}
Well, nothing gets printed to screen.
I am using a similar mechanism on another install which doesnt have clean url's turned on and so everything is done through node/$node/whatever and everything works fine.
Any help would really be appreciated as I have spent the best part of today on this and am no further forward.
Comments
Instead of '%content' try
Instead of '%content' try '%', there needs to be a function associated with '%content' for it to work. See Wildcard Loader Arguments for more information.
I had assumed that 'page
I had assumed that 'page callback' => 'testContent' was taking care of the callback. I will try anything though, so will go try your advice now.
testContent is the page
testContent is the page callback, named wildcards have their own callback.
Ok, well not sure is this is
Ok, well not sure is this is a step forward or not but upon your advice my callback function is now getting the name of node. Nothing else though. In previous developments when I have used this technique i would expect to get the entire node as an object. i.e. i could access it as $node->nid etc.
All i appear to be getting with this is a string with the node name.
_
If you had read the Wildcard Loader documentation that nevets pointed you to, you'd see that %node would call node_load()
Pete.
http://adaptive.co.uk
Yes, I did, bit node_load()
Yes, I did, bit node_load() accepts the nid. This is why i mentioned in my initial post about clean urls being on.
clean_urls should not be the
clean_urls should not be the issue. Given the path 'content/%content/test' what are you passing as %content?. If passing a node id (nid) the path would be 'content/%node/test' (note if you change menu entries in hook_menu you need to clear the menu cache).
You need to declare a
You need to declare a function called content_load() if you are going to use %content.
Full-time freelancer, always looking for work.
jaypan.com (my portfolio)
Hi nevets first thanks for
Hi nevets first thanks for the help so far. Well the problem was that I was using node/%node/test on a previous development and i in my callback function i had access to that node as an object. This made perfect sense and i never looked any further in to it.
On this development which is an existing one the nodes are set up differently and are using content/something
So my first attempt was to use content/%node/test
This gave me nothing at all.
I have tried several other things but I just cant get the node object as before. The closes I can get is to get the title of the node when using content/%/test. I could look up the node table and get the id and then load the node that way (i think) but then there may be more than 1 node with the same title in which case this is not correct. And anyway this seems messy.
I am fairly new to drupal so I am clearly missing something but I cant see what it would be.
Once again, what are you
Once again, what are you actually placing in the url/path for %content?
I was wondering the same
I was wondering the same thing, but I just went through the whole thread again, and I think I get it.
OP - you are confusing clean URLs with URL aliases. A clean URL changes this:
site.com/index.php?q=some/path
to
site.com/some/path
I think where you are confused is that you are probably using pathauto, which will take a path like this (assuming clean URLs is turned on):
site.com/node/23
and change it to
site.com/content/page-name
There are a couple of things you need to realize with this. The first is that the when you use pathauto to create path aliases, both paths exist. So accessing
site.com/node/23
will show you the same page as accessing
site.com/content/pate-name
That doesn't really solve your problem. What does solve your problem is that you can still use the same node load argument whether you are using pathauto or not. So you don't need
content/%content/test
You can continue using
content/%node/test
same as you were before, passing a NID in place of the wildcard.
I hope this helps you figure out your problem.
Full-time freelancer, always looking for work.
jaypan.com (my portfolio)
Hi, sorry to all involved in
Hi, sorry to all involved in the thread if i have confused the matter, as I said I am very new to Drupal. Jay, you are dead on with what my issue actually is. I investigated and pathauto is being used.
I have tried using content/%node/test but still i do not have access to the node objct. How do i explicitly pass the nid of %node ?
here is exactly what I have stripped my code down to to test this but I still cannot access any of the nodes data:
/**
* hook_menu() -
*
*/
function test_menu() {
$items ['content/%node/test'] = Array (
'title' => 'Test',
'type' => MENU_CALLBACK,
'access arguments' => Array ('view', 1),
'page callback' => 'displayTest',
'page arguments' => Array (1)
);
}
function displayTest($node)
{
print_r($node);
exit;
}
What is the URL you are using
What is the URL you are using to access the page?
well the url can be anything.
well the url can be anything. For example one page is content/thoughts-3 and another is content/thoughts-5 but they can be anything. The point is i want a universal method of being able to access any node followed by /test and be able to work with the node there.
this worked fine before using node/%node/test but i think he was definately correct before by saying the pathauto is throwing this all off.
If using "%node', the
If using "%node', the argument needs to be a nid, so unaliased paths would be of the form content/124, content/34 etc.
It looks like you are trying
It looks like you are trying to take an aliased path from somewhere, and you want to use that to build a link. This link will access the path in the code you showed us. So what you need to do is get the NID from the aliased path. To get the NID from an aliased path, you can use this little piece of code:
$path = 'content/thoughts-3'$nid_pieces = explode('/', drupal_get_normal_path($path));
$nid = $nid_pieces[1];
So now your links can be built in this pattern:
site.com/content/$nid/test
And in your hook_menu, you can define the path as:
$menu['content/%node/test']
which will now load the node object.
Full-time freelancer, always looking for work.
jaypan.com (my portfolio)
Or, you could stick with the
Or, you could stick with the same setup you have now, where you have:
$menu['content/%content/test'];
Then you define a loader function like this:
<?phpfunction content_load($title)
{
$path = 'content/' . $title;
$nid_pieces = explode('/', drupal_get_normal_path($path));
$nid = $nid_pieces[1];
return node_load($nid);
}
?>
This may be a simpler solution actually, as you don't need to change anything. But, it will be a less reliable solution, as if the pathauto defaults are changed, your function will stop working. The function will work regardless with the first method, as long as you pass the whole path into drupal_get_normal_path().
Full-time freelancer, always looking for work.
jaypan.com (my portfolio)