2

In Drupal 7, how can I add additional dynamic markup to a menu item? As an example, I'm trying to show the number of nodes 'needing attention' to the right of link in my primary navigation as noted in the attached graphic.

It seems like a theme function would be appropriate here, but I'm struggling to find which hook I should be implementing.

Example menu

4

Hope this will help you,

  • As answered theme_menu_link OR theme_menu_tree wil help you to do this. If you go through the above links you can get an idea to change the html of required menu item.

On page load:

  • for example im using theme_menu_link in template.php refer this code. Node count will be update on every page load, if any changes.

  • I have updated l() for you requirement. For example I have used a simple function , modify the function as per your requirement.

    $output = l($element['#title'] . '<span class="'.strtolower(strip_tags(str_replace(" ", "_", $element['#title']))).'_node_count">'.get_node_count($parms).'</span>', $element['#href'], array('html'=>TRUE));
    
    function get_node_count($parms){
        //you code goes here for get node count
        return $node_cnt;
        }
    

If you don't want on page load, it can also be done using ajax callback, but not preferable to make ajax callback again and again.

  • If you want to do refer ajax callback, then the only change has to be done in the l(). Add the unique menu id, which is used to show the node count for the corresponding menu id.

    $output = l($element['#title'] . '<span id="'.strtolower(strip_tags(str_replace(" ", "_", $element['#title']))).'_node_count"></span>', $element['#href'], array('html'=>TRUE));
    
    • On sucessfull ajax callback you have to update the node count for each menu. using
      ...
      success: function(data) { // Change node count $('#menu_title__node_count').html(data); }

    • You will get the class for each node count, you can style as required. And also you can add a common class for common style, based on your requirement.

| improve this answer | |
1

To alter the menu item markup, you can use:

Note: there are actually functions available for specific menus there too:

Targeted overrides (suggestions) available for theme_menu_link() and theme_menu_tree()

In addition to other changes to menu rendering, a Drupal 7 theme can implement a THEMENAME_menu_tree__MENU_NAME() and/or THEMENAME_menu_link__MENU_NAME() function to override theme_menu_tree()/theme_menu_link() for a specific menu. For example, THEMENAME_menu_link__management() would override theme_menu_link() for links within the "Management" menu. This is similar to how "node--article.tpl.php" overrides "node.tpl.php".

Source: https://drupal.org/node/254940#theme-suggestions-for-menus

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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