Professional Innovations

November 18th 2014 / Drupal

Drupal's Menu System

Posted by Alain Francis

The term “menu system” in Drupal is somewhat of a misnomer. It does not, as its name implies, enable us to just define menus with items and sub items; we can think of it as a way to provide callback mapping, access control and customization.

Callback Mapping
Suppose we want to run a function every time a specific URL is requested; thejob of this function is to produce the output for the page where the browser will land.
Inside your module (assuming its name is mymodule), let’s start by hooking the menu as follows:

function mymodule_menu()
{
$items[‘products’] = array (
                                                     ‘title’ => ‘Products’,
                                                ‘page callback’ => ‘display_products’,
‘access callback’ => TRUE,
                                                      ‘type’ => MENU_CALLBACK
                                                     );

  return $items;
}

The hook_menu() that we created above, defines an array of menu items (here we defined an array of just one item but we can add more items if we wish so) and returns it. Every item is defined in the array using a key; the key here is ‘products’.

Suppose your website’s domain name is mywebapp; every time you request http://mywebapp/products, the function named display_products() (that we will define next) will be invoked and the string that is returned by that function will be the page’s rendered text. The page’s title will be Products which is the value of the ‘title’ attribute. The ‘access callback’ attribute enables us to define a function that returns a boolean; if the value is true, the page callback function is executed and you can access the page; if the value is false, you get an access denied error message. It is up to you to write the code that implement your strategy and based on that, return either true or false. In this example, we simply set it to TRUE meaning that anyone can request the products page with no restrictions of any kind.
The last attribute is the most important to us; when we set the ‘type’ to MENU_CALLBACK, no entry will exist in any menu; we’re just informing Drupal that every time the products page is requested, execute the attached callback function. Another possible value would be MENU_NORMAL_ITEM; in this case, we will have an entry in the navigation menu that, when clicked, will take us to the products page and the callback function will be executed.

Let’s write the callback function as follows:

function display_products()
{
     $now = format_date(time());
     return t(‘It is now @now’, array(‘@now’ => $now));
}

When the products page is accessed, the page’s title will be Products and the page body will contain the sentence: It is now (the time).

Menu callbacks can take parameters; what if we request the products page as follows:  http://mywebapp/products/15. This might indicate that we want to display the information related to the product having id = 15. To handle this scenario, we need to redefine the page callbackfunction so that it takes 1 parameter (or many parameters if we liketo pass more than one parameter).

function display_products($product_id = null)
{
     if ($product_id == NULL)
          return t(‘Please, select a product to browse.’);
     else
          return t(‘You are browsing product ‘ . $product_id);
}

We made use of optional parameters so that, if you requested the page without specifying a product id as follows: http://mywebapp/products, the first message is displayed.