4

I want a page where a user can create 3 different content types : "Machine" , 'Equipment" or "Services" . My goal is to implement 3 tabs for each content type node creation form.

I implemented hook_menu like below :

function MyMODULE_menu() {

  $items['instrument'] = array(
    'title' => 'Instrument',
    'type' => MENU_NORMAL_ITEM,
    'menu_name' => 'main-menu',
  );

  $items['instrument/machine'] = array(
    'title' => 'Machine',
    'type' => MENU_DEFAULT_LOCAL_TASK,
    'page callback' => 'drupal_get_form',
    'page arguments' => 'product_node_form',
  );  
  $items['instrument/equipment'] = array(
    'title' => 'Equipment',
    'type' => MENU_LOCAL_TASK,
    'page callback' => 'drupal_get_form',
    'page arguments' => 'equipments_node_form',
  );  
  $items['instrument/service'] = array(
    'title' => 'Service',
    'type' => MENU_LOCAL_TASK,
    'page callback' => 'drupal_get_form',
    'page arguments' => 'member_profile_node_form',
  );

  return $items;
}

After enabling my module, I expected that the menu item should be in main menu, but it wasn't there and I got a 'page not found error' when targeting "mywebsiteaddress/instrument" .

Where is my mistake?

1 Answer 1

3

There are 2 things going on: you've not defined the permissions on the menu & you've not defined the page callback for when the 'instrument' path is accessed. Without this, Drupal doesn't know when or what to display for your 'instrument' menu item.

Doing something like this:

  $items['instrument'] = array(
    'title' => 'Instrument',
    'type' => MENU_NORMAL_ITEM,
    'menu_name' => 'main-menu',
    //@TODO: define a function called mymodule_instrument_page_callback 
    // that will render the page for the 'instrument' path.
    'page callback' => 'mymodule_instrument_page_callback',
    // Setting 'access arguments' => array('view content') 
    // will allow anyone with the 'view content' permission to view the menu item.
    //'access callback' can be used for dynamically determining access
    'access arguments' => array('view content'),
  );

Will make your menu item available. See the hook_menu() docs for information on page & access callbacks.

1
  • 2
    Since the OP also has a MENU_DEFAULT_LOCAL_TASK item, it would be better to use that page callback (and remove it from /instrument/machine). That way /instrument will render the form and the tab will fall back to that page as well (so both paths show the same content.). Apr 19, 2016 at 9:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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