Multiple pages on the same dynamic path (node types)

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 despatcher:

<?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

pl2 - 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. |

 
 

Drupal is a registered trademark of Dries Buytaert.