2

I'm trying to write my first module and am using the hook_menu to route the pages. The module consists of a multi-step form and two static content pages.

/**
 * Implements hook_menu().
 */
function order_menu() {
  $items = array();

  $items['order/form'] = array(
    'title' => 'Order Form',
    'description' => 'Order multistep form.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('order_form'),
    'access callback' => TRUE,
    'file' => 'order_form.inc',
    'tpe' => MENU_CALLBACK,
  );

  $items['order/cancel'] = array(
    'title' => 'Order Cancelled',
    'description' => 'Order cancelled.',
    'page callback' => 'order_cancel',
    'access callback' => TRUE,
    'file' => 'order_form.inc',
    'type' => MENU_CALLBACK,
  );

  $items['order/success'] = array(
    'title' => 'Order Success',
    'description' => 'Order Successful.',
    'page callback' => 'order_success',
    'access callback' => TRUE,
    'file' => 'order_form.inc',
    'type' => MENU_CALLBACK,
  );

  return $items;
}

What I want to do is be able to access 'example.com/order/form' from 'example.com/order' but when I change the menu hook from $items['order/form'] to $items['order'] it displays as page not found.

Any suggestions as to how to how to get the module to display the content I want from example.com/order?

2
  • on /order you want it to display the same form as /order/form or do you want to replace /order/form with simply /order?
    – electblake
    Mar 17 '11 at 15:59
  • I would like /order to display the same form as /order/form. I tried to accomplish that by changing /order/form to /order, but I get page not found.
    – cbaigorri
    Mar 17 '11 at 16:03
2

Did you force Drupal to rebuild the menu, after you changed the code? If you don't do that, Drupal will use the previously defined menus.

To force Drupal to rebuild the menus, you just need to visit the modules page (the administration page showing the list of all installed modules), disable a module, and re-enable it. The last two operations are not strictly necessary; you would probably get the same effect if you click on the "Save" button present on that page without to enable or disable any module at all.
Drupal will call any implementation of hook_menu() and hook_menu_alter(), and it will grab the last menu callbacks defined from your module.

Alternatively, you can call menu_rebuild() from code, or install Devel, which has a block containing a link that allows to force Drupal to rebuild the menu list it caches.

3
  • Install the Devel module, makes working with hook_menu() almost pleasurable :)
    – electblake
    Mar 17 '11 at 16:23
  • disabling the module and re-enabling it did the trick. I was clearing all caches, but I guess that doesn't rebuild the menus. Thanks!
    – cbaigorri
    Mar 17 '11 at 16:24
  • I really suggest installing Devel (or drush, drush cc all does rebuild the menu too). Also you don't need to re-enable the module, it is enough if you just submit the module selection form without any changes.
    – Berdir
    Mar 17 '11 at 16:50
2

The problem is that order is not defined as a menu item. An easy way of fixing this would be to include it in the hook menu definition:

/**
 * Implements hook_menu().
 */
function order_menu() {
  $items = array();

  $items['order'] = $items['order/form'] = array(
    'title' => 'Order Form',
    'description' => 'Order multistep form.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('order_form'),
    'access callback' => TRUE,
    'file' => 'order_form.inc',
    'type' => MENU_CALLBACK,
  );

  ...

}

You could use use the MENU_DEFAULT_LOCAL_TASK for the order/form menu item, but I believe it will produce a tab by default, which might not be what you want.

Remember to clear the cache after making these changes.

1
  • double declaration - NEAT! I was going to recommend duplicating the menu item but this is cleverer ;)
    – electblake
    Mar 17 '11 at 16:24

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.