PHP, MySQL, Drupal, .htaccess, Robots.txt, Phponwebsites: menu_normal_item
menu_normal_item - phponwebsites.com
Showing posts with label menu_normal_item. Show all posts

22 Dec 2015

Drupal 7 - Create menu tab programmatically

    This blog describes about how to create menu tab programmatically in drupal 7. We can create menu items using hook_menu().

Create menu tab programmatically

Menu tab creation in Drupal 7:


     Consider below code snippet to create menu tab in drupal 7.

/**
 * Implement hook_menu()
 */
function phponwebsites_menu() {

  $items['test'] = array(
    'title' => t('Create Menu Tab'),
    'page callback' => 'testpage_tab1',
    'access callback' => TRUE,
  );

  $items['test/tab1'] = array(
    'title' => t('First Tab'),
    'type' => MENU_DEFAULT_LOCAL_TASK,
    'page callback' => 'testpage_tab1',
    'access callback' => TRUE,
  );

  $items['test/tab2'] = array(
    'title' => t('Second Tab'),
    'type' => MENU_LOCAL_TASK,
    'page callback' => 'testpage_tab2',
    'access callback' => TRUE,
  );

  $items['test/tab3'] = array(
    'title' => t('Third Tab'),
    'type' => MENU_LOCAL_TASK,
    'page callback' => 'testpage_tab3',
    'access callback' => TRUE,
  );

  return $items;
}

/**
 * Implement testpage_tab1()
 */
function testpage_tab1() {
  $str = t('Hi this is first tab');
  return $str;
}

/**
 * Implement testpage_tab2()
 */
function testpage_tab2() {
  $str = t('Hi this is second tab');
  return $str;
}

/**
 * Implement testpage_tab3()
 */
function testpage_tab3() {
  $str = t('Hi this is third tab');
  return $str;
}


Where,
   In hook_menu,
      Title – page title
      Type – type of menu item,
       MENU_CALLBACK, MENU_DEFAULT_LOCAL_TASK, MENU_LOCAL_TASK, MENU_LOCAL_ACTION, MENU_NORMAL_ITEM, MENU_SUGGESTED_ITEM are types of
menu item in drupal 7.

MENU_CALLBACK – register path for a menu item
MENU_DEFAULT_LOCAL_TASK – default tab for a menu
MENU_LOCAL_TASK – additional tabs for menu
MENU_LOCAL_ACTION – actions for menu items
MENU_NORMAL_ITEM – add menu item into any menus like main_menu, user_menu
MENU_SUGGESTED_ITEM – module may suggest menu items

Page callback – callback for a menu
Access callback – who can access the page

        Now i’ve hope you should know how to create menu tab programmatically in drupal 7.

18 Dec 2015

Drupal 7 – Add class into menu item using hook_menu()

  This blog describes about how to add class into menu item that is created programmatically  using hook_menu() in drupal 7.
        We know how to add custom menu item into already created menu in drupal7. Is it possible to add class to that menu item in durpal 7? Yes you can add custom classes into menu item using hook_menu() in drupal 7.

Add class into menu item in drupal 7:

     
       Consider below program to add class into menu item in drupal 7.

/**
 * Implement hook_menu()
 */
function phponwebsites_menu() {
  $items['sample'] = array(
    'title' => t('Sample page'),
    'type' => MENU_NORMAL_ITEM,
    'menu_name' => 'main-menu',
    'page callback' => 'samplepage',
    'access callback' => TRUE,
    'options' => array(
      'attributes' => array(
        'class' => array('drupal-menu-class')
      )
   ),
  );

  return $items;
}

/**
 * Implement samplepage()
 */
function samplepage() {
  $str = t('Hi this is sample page');
  return $str;
}

Where,
     type – MENU_NORMAL_ITEM
     menu-name – name of the menu to add new link
     options – add any attributes like class, id

You need to clear cache to see created menu items with custom class into main menu.

Add multiple classes into menu item in drupal 7:


     Similarly you can add multiple classes into menu item using hook_menu() in drupal 7. Consider below program to add multiple
classes into menu item.

function phponwebsites_menu() {
  $items['sample'] = array(
    'title' => t('Sample page'),
    'type' => MENU_NORMAL_ITEM,
    'menu_name' => 'main-menu',
    'page callback' => 'samplepage',
    'access callback' => TRUE,
    'options' => array(
      'attributes' => array(
        'class' => array('drupal-menu-class' ,  'drupal-menu-new-class')
      )
   ),
  );

  return $items;
}

       Now i’ve hope you know how to add class programmatically to menu item using hook_menu() in drupal 7.

16 Dec 2015

Drupal 7 - add link into menu programmatically using hook_menu()


     This blog describes about how to add a new menu item into menu like main menu, user menu in drupal 7.


drupal 7 - add link into menu programmatically using hook_menu()

     
We can create a menu item using hook_menu in drupal 7. Can we add menu item into already created menu in drupal7? Yes you can add a link into menu using hook_menu().

Add new menu item into main menu in drupal 7:


       Consider below program to add new menu item into main menu in drupal 7.

/**
 * Implement hook_menu()
 */
function phponwebsites_menu() {
  $items['sample'] = array(
    'title' => t('Sample page'),
    'type' => MENU_NORMAL_ITEM,
    'menu_name' => 'main-menu',
    'page callback' => 'samplepage',
    'access callback' => TRUE,
  );

  return $items;
}

/**
 * Implement samplepage()
 */
function samplepage() {
  $str = t('Hi this is sample page');
  return $str;
}


Where,
     type – MENU_NORMAL_ITEM
     menu-name – name of the menu to add new link
  
     You need to clear cache to see created new menu item in main menu. Now i’ve hope you know how to add new link programmatically to already created menu in drupal 7.