How to modify a primary link with HTML content in real time

This is how you can apply changes to a link in the primary_links section, for instance if you want to append a badge with a number of items for that specific view.
The code can be applied in the template.php of your theme.

<?php
function MY_THEME_NAME_links($links, $attributes = FALSE) {
 
   if(
$links){
       
$badge_data = mymodule_get_badge_data();
        foreach (
$links as $key => $link) {
           
//if($link['href'] == 'my_path'){
           
if(in_array($link['href'], $badge_data['paths'])) {
               
//$links[$key]['title'] .= "<span class='badge'>43</span>";
               
$links[$key]['title'] .= "<span class='badge'>".$badge_data['values'][$link['href']]."</span>";
               
$links[$key]['html'] = TRUE;
            }
        }
    }
 
 
// Process primary links normally.
 
return theme_links($links,$attributes);
}

//Then  in the CSS file you add:
.menu li span.badge {
   
position: absolute;
   
top: -8px;
   
background-color: #da3333;
   
border-radius: 5px;
   
padding: 2px;
   
line-height: 12px;
   
min-width: 14px;
   
text-align: center;
}
?>

The result will look something like this:

Knowledge keywords: