Multiple pages on the same dynamic path (node types)

Last modified: August 7, 2009 - 20:10

If you want to fire different access or page callbacks on the same dynamic path, typically node/123 or node/123/foo depending on the node type, you will face the problem that the menu system can only hold an entry for node/%/foo. Let's say we want to register a path which answers on types article and story We will register node/%mymodule_article_story

<?php
function mymodule_article_story_load($arg) {
  if (!
is_numeric($arg)) {
    return
FALSE;
  }
  if (
$node = node_load($arg)) {
    if (
$node->type == 'article' || $node->type == 'story') {
      return
$node;
    }
  }
  return
FALSE;
}
?>

and then we want to fire page callbacks, so we write a small dispatcher:

<?php
function mymodule_page($node) {
 
// $node->type can only be story or article.
 
return $node->type == 'article' ? mymodule_page_article($node) : mymodule_page_story($node);
}
?>

and write the menu entry as

<?php
$items
['node/%mymodule_article_story/foo'] = array(
 
'title callback' => 'mymodule_title_callback';
 
'title arguments' => array(1),
 
'page callback' => 'mymodule_page',
 
'page arguments' => array(1),
 
'access callback' => 'node_access',
 
'access arguments' => array(1),
);
?>

mymodule_title_callback?

drewish - March 18, 2008 - 22:48

i guess you'd still need to define 'mymodule_title_callback'?

Doesn't registering

Pedro Lozano - May 6, 2008 - 14:21

Doesn't registering 'node/%mymodule_article_story' will also disable 'node/%node' ?

Let's say your module want to manage story & article node types but want to leave de default 'node/%node' menu item to handle the rest. Is it possible?

You can always fall back

chx - May 30, 2008 - 13:29

You can record somewhere what the original entry was before you menu_alter'd the entry and fall back.
--
The news is Now Public | Drupal development: making the world better, one patch at a time. | A bedroom without a teddy is like a face without a smile. |

Not following you...

mercmobily - August 31, 2008 - 15:40

Hi,

I am a little thick. What do you mean chx?

Merc.

Not working in my situation...

gsadler - April 22, 2009 - 22:21

I have two node types that share the same path: node/%/admin

I've set it up this way: two menu items for each node type, node/%node_type_1/admin and node/%node_type_2/admin. Each node_type_load function checks the $node->type and returns false if it's not the correct node type.

The first menu item gets saved in menu_router, but not the second so when trying to access node/%node_type_2/admin I get a page not found error.

I've reset the cache and know that there is some name collision because if I change the 3rd argument on the second node type (say from admin to admins) it loads the page fine and gets saved in the db.

Is this a bug or is it intended behavior?

I faced a similar issue. I

rjl - April 22, 2009 - 22:30

I faced a similar issue. I wanted to uses 'submissions' as the 3rd element of my path (node/%node/submissions). I also have the webform module installed which uses that same path. I couldn't figure out how to resolve it, so I ultimately changed my 3rd element to something else.

So I wonder if you couldn't change one of your paths from "admin" to something else?

Yeah that's the easiest solution...

gsadler - April 23, 2009 - 02:28

But I'm stubborn and type a. I'll probably take your advice, but this issue is driving me insane.

 
 

Drupal is a registered trademark of Dries Buytaert.