7.x menu.inc theme_menu_link(array $variables)

Returns HTML for a menu link and submenu.

Parameters

$variables: An associative array containing:

  • element: Structured array data for a menu link.

Related topics

2 theme calls to theme_menu_link()
MenuTreeOutputTestCase::testMenuTreeData in modules/simpletest/tests/menu.test
Validate the generation of a proper menu tree output.
menu_tree_output in includes/menu.inc
Returns an output structure for rendering a menu tree.

File

includes/menu.inc, line 1636
API for the Drupal menu system.

Code

function theme_menu_link(array $variables) {
  $element = $variables['element'];
  $sub_menu = '';

  if ($element['#below']) {
    $sub_menu = drupal_render($element['#below']);
  }
  $output = l($element['#title'], $element['#href'], $element['#localized_options']);
  return '<li' . drupal_attributes($element['#attributes']) . '>' . $output . $sub_menu . "</li>\n";
}

Comments

khanz’s picture

Nice snippet!! But how to check multiple paths?

>
if ($variables['element']['#original_link']['link_path'] == 'path1' || 'path2')

Or operator as in above code doesnt works, any hint?

zelenovas’s picture

In PHP to check multiple values you need somethink like this:

if ($variables['element']['#original_link']['link_path'] == 'path1' || $variables['element']['#original_link']['link_path'] == 'path2') {
    // some code
}

OR

if (in_array($variables['element']['#original_link']['link_path'], array('path1', 'path2'))) {
    // some code
} 

See more about function "in_array" in manual: https://php.net/manual/en/function.in-array.php

web506’s picture

how to Print a Menu Title in TPL?? Just the Name of the Menu.

killua99’s picture

Still quite useful

asghar’s picture

If you want to override some specific menu li output so you can use suggested theme hook like this

<?php
function [theme_name]_menu_link__[your_menu_name](array $variables) {
  //dsm($variables);
  $element = $variables['element'];
  $sub_menu = '';

  if ($element['#below']) {
    $sub_menu = drupal_render($element['#below']);
  }
  $output = l($element['#title'], $element['#href'], $element['#localized_options']);
  return '<li' . drupal_attributes($element['#attributes']) . '>' . $output . $sub_menu . "</li>\n";
}
?>
mpotter’s picture

Note that the theme hook passes $element['#href'] to the l() function. Thus, for *local* links, #href is really the "path" rather then the fully expanded "href url".

Be careful using the url() function to set the value of $element['#href'] since when this is passed to l() you can end up with the base_url subdirectory path getting duplicated in the final url link. In other words remember that l() already calls url() so you don't want to do it twice.

hardik jayeshbhai hihoriya’s picture

function themename_menu_link(array $variables) {
    $element = $variables['element'];
    $sub_menu = '';
    $caret = '';

    if ($element['#below']) {
        // Prevent dropdown functions from being added to management menu so it
        // does not affect the navbar module.
        if (($element['#original_link']['menu_name'] == 'main-menu') && (module_exists('navbar'))) {
            $sub_menu = drupal_render($element['#below']);
        } elseif ((!empty($element['#original_link']['depth'])) && ($element['#original_link']['depth'] >= 1)) {
            // Add our own wrapper.
            unset($element['#below']['#theme_wrappers']);
            $sub_menu = '';
            // Generate as standard dropdown.
            //$element['#title'] .= ' ';
            $name_class = drupal_html_class(strip_tags(trim($element['#title'])));
            $caret = '';
            $element['#attributes']['class'][] = 'dropdown ' . $name_class;
            $element['#localized_options']['html'] = TRUE;

            // Set dropdown trigger element to # to prevent inadvertant page loading
            // when a submenu link is clicked.
            //$element['#localized_options']['attributes']['data-target'] = '#';
            //$element['#localized_options']['attributes']['class'][] = 'dropdown-toggle';
            //$element['#localized_options']['attributes']['data-toggle'] = 'dropdown';
        }
    }
    // On primary navigation menu, class 'active' is not set on active menu item.
     if (($element['#href'] == $_GET['q'] || ($element['#href'] == '' && drupal_is_front_page())) && (empty($element['#localized_options']['language']))) {
        $element['#attributes']['class'][] = 'active';
    }
    $output = l($element['#title'], $element['#href'], $element['#localized_options']);
    return '
  • ' . $output . $caret . $sub_menu . "
  • \n"; } /*using dkkkk*/

    Pages