4

In simple terms, what is hook_menu_alter for? I understand callbacks in general, but I'm not quite sure if this method is misnamed in a sense, as I see it used in the context of permissions rather than menus. I'm probably just confused.

4

I think that your confusion comes from the fact that Drupal 6 and 7 mixes menu and routing. One set of hooks for both purposes, and here you have problems. If this hook was named hook_route_alter(), using it to alter permissions to some routes would be obvious, right? So that's what modules do, using the name they got. Personally I agree it's not most lucky naming, but once you remember that in this context, basically, menu == routing, it all becomes much more intuitive.

3

Change theme of particular page/menu:

There we have used hook_custom_theme() hook to change theme on particular page. Instead of this we can also use hook_menu_alter() as another way to achieve same feature.

/*
 * Implements hook_menu_alter().
 */
function custom_module_menu_alter(&$items){
  // Change 'node/add/page' page to admin theme (Assume by default node/add page uses default theme)
  $items ['node/add/page']['theme callback'] = variable_get('admin_theme');
}

Change title of page/menu:

Menu provides page titles. If we are in ‘node/add/page’ then we see page title as ‘Create Basic page’ but what if we need custom title like ‘Add page’. We can achive this using below custom module.

/*
 * Implements hook_menu_alter().
 */
function custom_module_menu_alter(&$items){
  // Change 'node/add/page' title to 'Add page'
  $items ['node/add/page']['title'] = 'Add Page';
}

Customize accessing a page/menu:

For many pages/menus we restrict access to different users or on conditions. Let’s say we want restrict ‘user/register’ page in particular times.. I mean no registration will be done in 12AM - 06AM.

/*
 * Implements hook_menu_alter().
 */
function custom_module_menu_alter(&$items){
  // Restrict 'user/register' page in 12AM-05AM
  $items ['user/register']['access callback'] = '_MY_MODULE_time_access';
}

    /*
     * Access callback function to restrict user registration in particular timings
     */
    function custom_module_time_access(){
      // Get current time hour like 0,1,2,3....23
      $current_time_hour = date('G');
      
      // if current time is between 0 & 5 then return false
      if($current_time_hour >= 0 && $current_time_hour <= 5 ){
        return false;
      }
      return TRUE;
    }

Create duplicate page/menu:

We can also use hook_menu_alter() hook if we want to create a duplicate page, a page with same functionality but with different url. Let’s say we want a url ‘add/page’ which acts same as of ‘node/add/page’.

/*
 * Implements hook_menu_alter().
 */
function custom_module_menu_alter(&$items){
  // Copy ‘node/add/page’ page to ‘add/page’
  $items ['add/page'] = $items ['node/add/page'];
}
-1

With hook_menu_alter you can manipulate $items: Associative array of menu router definitions returned from hook_menu(), you can add or remove items or manipulate menu item properties : https://api.drupal.org/api/drupal/modules%21system%21system.api.php/function/hook_menu/7

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.