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

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.