[SOLVED] How to Programmatically Create Menu Tabs?
https://www.zyxware.com/sites/default/files/styles/user_image/public/default_images/index.png?itok=2YmREnrP
BY wellme.m
1 year ago
Drupal-7
0
comments comment

For a recent project, I had a requirement to implement menu tabs in Drupal 7. Let see what are the necessary code for the same. First I created menu tabs in .module file using hook_menu() with the following codes.

/**
 * Implements hook_menu().
 */
function MYMODULE_menu() {
  $items['path']= array(
    'title' => t('Create menu tab in your page.'),
    'page callback' => 'first_tab',
    'access callback' => TRUE,
    'weight' => 0,
  );

  $items['path/firsttab'] = array(
    'title' => t('Create first tab.'),
    'type' => MENU_DEFAULT_LOCAL_TASK,
    'page callback' => 'first_tab',
    'access callback' => TRUE,
    'weight' => 0,
  );

  $items['path/secondtab'] = array(
    'title' => t('Create second tab'),
    'type' => MENU_LOCAL_TASK,
    'page callback' => 'second_tab',
    'access callback' => TRUE,
    'weight' => 1,
  );

  $items['path/thirdtab'] = array(
    'title' => t('Create third tab'),
    'type' => MENU_LOCAL_TASK,
    'page callback' => 'third_tab',
    'access callback' => TRUE,
    'weight' => 2,
  );

  return $items;
}

Then I wrote the necessary page callback functions for the corresponding tabs.

/**
 *  First tab.
 */
  function first_tab() {
  $str = t('Hi this is first tab');
  return $str;
}

/**
 *  Second tab.
 */
  function second_tab() {
  $str = t('Hi this is second tab');
  return $str;
}

/**
 *  Third tab.
 */
  function third_tab() {
  $str = t('Hi this is third tab');
  return $str;
}

Similar to the above, you can create more tabs and add other functionalities for each page. Here 'MENU_DEFAULT_LOCAL_TASK' and 'MENU_LOCAL_TASK' in menu type specification is important. The main page tab should be specified as 'MENU_DEFAULT_LOCAL_TASK'. Hope this is useful, contact us for further assistance.


RELATED ARTICLE

more_horiz
close

on 24th January 2012 / by webmaster
By default Drupal 7 has two types of views - The Full node view and the Teaser view. This has been the case for all previous versions of Drupal. However if your requirements are different, then you might have to create a custom view that is tailored to meet your specific needs. Check out how to create a simple custom “View mode” for nodes in Drupal 7. Web Development Drupal Drupal Development Drupal 7 Leave a reply Your email address will not be published. Required fields are marker * uk (not verified) access_time 17 Aug 2019 - 20:29 Hi, It would be good if you rename the preprocess function tdbcustom_preprocess_node to CustomModule_preprocess_node Thanks, UK. webmaster access_time 17 Aug 2019 - 20:29 In reply to Hi,It would be good if you by uk (not verified) Thanks for the suggestion. Corrected the module name in the sample code. Add new comment
Leave a reply
Your email address will not be published. Required fields are marker *

Filtered HTML

  • Web page addresses and email addresses turn into links automatically.
  • Allowed HTML tags: <a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type='1 A I'> <li> <dl> <dt> <dd> <h2 id='jump-*'> <h3 id> <h4 id> <h5 id> <h6 id>
  • Lines and paragraphs break automatically.
The content of this field is kept private and will not be shown publicly.
CAPTCHA This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.