home forums resources search newsjoinmembers: 6954
Sections PHP Flash Java Ruby Windows Linux

Cached part

This is very simple, a search-and-replace operation. Here is a list of changes:

  • hook_menu() no longer takes any parameters (remove $may_cache).
  • The value of path is the new index for $items.
  • callback becomes page callback
  • callback arguments becomes page arguments
  • access becomes access callback and access arguments.

    For example, access => user_access('administer nodes') becomes 'access callback' => 'user_access', 'access arguments' => array('administer nodes').

    However, the default for 'access callback' is 'user_access' so you can leave that out.

    Complex access things must be moved to a function which can be called on runtime, user_is_anonymous and user_is_logged_in are useful helpers. See Access control for more.

  • The title and description arguments should not have strings wrapped in t(), because translation of these happen in a later stage in the menu system. This allows translation of menu items to any language required on a site, adapting to the language used on the page.

Non-cached part

Let's suppose you had

<?php
/**
  * Implementation of hook_menu().
  */
  
function aggregator_menu($may_cache) {
  
if (!$may_cache) {  
    
if (arg(0) == 'aggregator'   &amp;&ampis_numeric(arg(2))) {
      
if (arg(1) == 'sources') {  
        
$feed 
aggregator_get_feed(arg(2));  
        
if ($feed) {  
          
$items
[] = array('path' =  &gt'aggregator/sources/'$feed['fid'] .'/configure',
            
'title' =  &gtt('Configure'),
            
'callback' =  &gt'drupal_get_form',
            
'callback arguments' =  &gt; array('aggregator_form_feed'$feed),
            
'access' =  &gtuser_access('administer news feeds'),
            
'type' =  &gtMENU_LOCAL_TASK,
            
'weight' =  &gt1);
        
}  
      
}  
    
}  
  
}  
  
  
return $items;  
  }
?>

this becomes

<?php
/**
  * Implementation of hook_menu().
  */
  
function aggregator_menu() {
  
$items
['aggregator/sources/%aggregator_feed/configure'] = array(  
    
'title' =  &gt'Configure',
    
'page callback' =  &gt'drupal_get_form',
    
'page arguments' =  &gt; array('aggregator_form_feed'2),
    
// NOTE: as of Drupal 6.2, all menu items are *required* to have  
    
// access control.  
    
'access arguments' =  &gt; array('administer news feeds'),
    
'type' =  &gtMENU_LOCAL_TASK,
    
'weight' =  &gt1,
  
);  
  
  
return $items;  
  }
?>

If we would use just a percent sign aggregator/sources/%/configure it'd match everything, including non-numeric values as well, like aggregator/sources/foo/configure. However, with %aggregator_feed we ask for a feed to be loaded based on the third argument and anything that's not a valid feed will lead to a 404.

About the use of wildcards, foreach() loops and MENU_ITEMs

In Drupal 5, we used to use foreach() loops to recursively declare several MENU_ITEMs or MENU_SUGGESTED_ITEMs.

In Drupal 6, we only need one entry in hook_menu() with the use of a proper wildcard. Additionally, you can create as many menu items (enabled or not) as you see fit with menu_link_save().

For a detailed discussion on this topic, see this issue.

Additional Run Once Code

Non menu code that was placed in hook_menu !$may_cache so that it could be run during initialisation, should now be moved to hook_init. Previously we called hook_init twice, once early in the bootstrap process, second just after the bootstrap has finished. The first instance is now called boot instead of init.

If you were using a loop in your menu code then you likely need menu_link_save.

A horribly complex example can be found in the menu of http://drupal.org/files/issues/akismet.d6-port.patch. It involves just about every possible path manipulation trick one could ever need for the path akismet/%akismet/%/'. $op, and then some. It passes the real page callback in $map[0] which is a bit of a hack, but is also quite useful.

printer-friendly version

Hiveminds's picture
This article brought to you by the Hiveminds Magazine - Staff. Contact us if you want to post an article or announcement anonymously
Thoughtbox - So what did you think?



 
 
Content Management Systems Silverlight Drupal Adobe Flex eRuby PHP Drupal
 videos
 articles
 blogs
 comments
 downloads
sitemap

Newsletter

Get updates on Hiveminds services, articles and downloads by signing up for the newsletter.

Editor's choice

Some of the better articles, stories and tutorials found at Hiveminds.

Find more

Find more of Hiveminds articles, stories, tutorials and user comments by searching.




Picked links

Hand picked websites and articles from around the web that provide quality reading.

page top