Search code examples
drupalmenupathdrupal-6

Menu path with wildcard


You can't use wildcards in menu paths? A quick summary of my problem (which I've made sure makes sense, so you're not wasting your time): I have a menu which i'm showing on node pages of a certain content-type. My path to a node page would be like...

events/instal2010

...where instal2010 would be the name of an event (event is the content-type).

I'm using the Context and Menu Block modules to place a menu in the sidebar on that page...

  • Event (the default active item)
  • Programme
  • Visitor info
  • Book tickets

... where the path for Programme would be

events/instal2010/programme

So for this to work for many different events, those menu items need a wildcard in their path, e.g.

events/*/programme

Perhaps it's time to ditch menus and just use a block with php to determine what page we're on from the URL.

Any advice from experienced hands would be awsome, thanks.


Solution

  • You cannot create menu items with wildcards from the administrative interface of Drupal, but you can create menu items with wildcards in a module. I would recommend creating a custom module that uses hook_menu() to create the menu items. An example implementation would look something like:

    function YOURMODULE_menu() {
      $items = array();
    
      $items['events/%/programme'] = array(
        'title' => 'Programme',
        'description' => 'Loads a program page',
        'page callback' => 'YOUR CUSTOM FUNCTION NAME', // Custom function used to perform any actions, display the page, etc
        'page arguments' => array(1),  // Passes wildcard (%) to your page callback function
        'access callback' => TRUE, // Change if you want to control access
        'type' => MENU_NORMAL_ITEM, // Creates a link in the menu
        'menu_name' => 'primary-links' // Adds the link to your primary links menu, change if needed
      );
    
      return $items;
    }
    

    In $items['events/%/programme'] = array(, the % is the wildcard and it will be passed to your page callback function. It may be helpful to read more about hook_menu() and the Anatomy of hook_menu may also help as well.