Skip to main content

Example of hook_menu()

Example of hook_menu()

Adding a module configuration or settings page to the main Drupal menu is an important task and I like to add a page for each module I build...
Even if it's more of a 'help' page to explain what the module does or how to use it.

Adding one page is fairly simple....

/**
 * Impliment hook_menu().
 */
function MY_MODULE_NAME_menu() {
  $items = array();
  
  // Configuration Page
  $items['admin/config/MY_MODULE_NAME'] = array(
    'title' => 'MY MODULE NAME',				// Menu item title
	'description' => 'Configuration of MY MODULE NAME.',	// Menu item description
	'access arguments' => array('administer users'), 	// Specify 'who' can access the page
	'page callback' => 'drupal_get_form', 			// Calls the Drupal Form API
	'page arguments' => array('MY_MODULE_NAME_form'), 	// Pass the function to pass to Form API
	'type' => MENU_NORMAL_ITEM,	
  );
    
  return $items;
}

Note that you'll need the function called in 'page arguments' which can be found here Example of hook_form() and hook_form_submit().

Adding Sub Menus

However, the really interesting feature is the ability to have sub menus and pages that are only available to different roles... See Example of hook_permission() to find out how to set permissions.

Here is an example of a menu item that has one sub, for a settings page, that has a different access level...

/**
 * Implementation of hook_menu().
 */
function MY_MODULE_NAME_menu() {
  
  $items = array();
  
  // First Item in the menu
  $items['admin/config/MY_MODULE_NAME'] = array(
    'title' => 'MY MODULE NAME',				// Menu item title
	'description' => 'View MY MODULE NAME.',		// Menu item description
	'access arguments' => array('view MY_MODULE_NAME'), 	// Specify 'who' can access the page
	'page callback' => 'MY_MODULE_NAME_display', 		// Specify a function to build the page
	'type' => MENU_NORMAL_ITEM,				// Use 'MENU_NORMAL_ITEM' for the first item
  );
  
  // This is an exact duplicate of the above except for the 'type', 'weight' and url,
  // which allows another page (settings) to be added as a sub to this one.
  $items['admin/config/MY_MODULE_NAME/display'] = array(
    'title' => 'MY MODULE NAME',				// Menu item title
	'description' => 'View MY MODULE NAME.',		// Menu item description
	'access arguments' => array('view MY_MODULE_NAME'), 	// Specify 'who' can access the page
	'page callback' => 'MY_MODULE_NAME_display', 		// Specify a function to build the page
	'type' => MENU_DEFAULT_LOCAL_TASK,			// This allows sub menus from this item.	
	'weight' => 0,						// Weights to help organise the sub menus
  );
  
  // This is a sub menu that sits under the above.
  // Notice that we have a different access rule so only different roles can view or change the settings.
  // Under 'page callback' we are calling the 'drupal_get_form' Form API to handle the settings admin page.
  $items['admin/config/MY_MODULE_NAME/settings'] = array(
    'title' => 'Settings',						// Menu item title
	'description' => 'MY MODULE NAME settings.',			// Menu item description
	'access arguments' => array('administer MY_MODULE_NAME'),	// Specify 'who' can access the page
    	'page callback' => 'drupal_get_form',				// Calls the Drupal Form API
    	'page arguments' => array('MY_MODULE_NAME_admin'), 		// Pass function to Form API
    	'file' => 'includes/MY_MODULE_NAME.admin.inc',			// You can include a file.
	'type' => MENU_LOCAL_TASK,					// This makes this a sub.
	'weight' => 1,							// Use 'weight' to help organise.
  );
    
  return $items;
}
Related Articles
  • Example of hook_permission()
  • Example of hook_form() and hook_form_submit()
  •  
Main Category
  • Module Building
  •  
Secondary Categories
  • Drupal
  • Drupal 7
  •  
 
Jon Moore

Search form

Jon Moore

Tips 'n' Snips

 
  • Home
  • General
  • Drupal
    • Drupal 6
    • Drupal 7
    • Module Building
  • HTML & CSS
  • Javascript
  • jQuery
  • PHP
    • CodeIgniter
    • Handy Functions
    • MySQL
  • WordPress
  • About

BBC Technology News

  • Kashmir users kicked off WhatsApp
  • Heavy fine for Chinese firm over unlicensed game
  • Uber had 6,000 US sexual assault reports in two years
  • Electric eel lights up Christmas tree and other news
  • Rory Cellan-Jones: Reporting the news with Parkinson's
More

Sitepoint

  • The Real Future of Remote Work is Asynchronous
  • 7 Ways Developers Can Contribute to Climate Action
  • How to Divert Traffic Using IP2Location in a Next.js Website
  • 10 Zsh Tips & Tricks: Configuration, Customization & Usage
  • Building a Habit Tracker with Prisma, Chakra UI, and React
More

Heart Internet

  • Sponsor Neil Hunter’s amazing Antarctic expedition – for Diabetes UK
  • 15 web design books of 2019 that you should read
  • 12 podcasts for every creative person’s commute
  • Ten TED Talks every designer and web creative should watch before 2019 is over
  • The rise of the robots: Will AI take over graphic design?
More
 

Backend Coders

  • PHP
  • Handy PHP Functions
  • PHP MySQL

Frontend Coders

  • HTML & CSS
  • jQuery
  • Javascript

CMS/CMF Systems

  • Drupal
  • Drupal 6 Specific
  • Drupal 7 Specific
  • Drupal Module Building
  • WordPress

About

  • Home
  • About
 

© Jon Moore 2019

All stock images are from www.istockphoto.com