In Drupal 7, how I can intercept menu rendering in template.php and add pipes after menu links?
I tried using template_preprocess_menu_tree but I don't think that is what I am looking for. I want to change the HTML structure of a couple of menus.
theme_menu_link() did the trick. It contains an array under element of #original_link
which has the menu name in it. Using that, I can add pipe delimited links to certain menus.
http://api.drupal.org/api/drupal/includes--menu.inc/function/theme_menu_link/7
This may be helpful. You can use preprocess functions to change or add something to menu link.
function THEME_preprocess_menu_link(&$variables) {
$variables['element']['#below']['#markup'] = ' <span>→</span>';
}
In this code span added to every navigation link item.
Personally I'd just add the pipe with CSS - you don't really want the pipes in your markup as they're purely presentational:
.breadcrumb li:after {
content: "|";
margin: 0 5px; // optional styling nicety
}
.breadcrumb li:last-child::after {
content: "";
}
Menus are themed with the theme_links function. You'll want to create a copy in your [themes folder]/MYTHEME/template.php and rename it to MYTHEME_links. You can find the default function for drupal 7 by visiting: http://api.drupal.org/api/drupal/includes--theme.inc/function/theme_links/7
Now you'll be able to customize how the menus look from the template.php file.
I recommend to use a plugin for this. Full tutorial here: Create a menu link with html markup in drupal 8
namespace Drupal\MYMODULE\Plugin\Menu;
use Drupal\Core\Menu\MenuLinkDefault;
/**
* A menu link that displays number of points.
*/
class MyMessagesMenuLink extends MenuLinkDefault {
/**
* {@inheritdoc}
*/
public function getTitle() {
$count = 0;
if(\Drupal::currentUser()->isAuthenticated()) {
// Load in your count here
...
}
return $this->t('My messages <span class="badge badge-dark">@count</span>', ['@count' => $count]);
}
/**
* {@inheritdoc}
*/
public function getCacheMaxAge() {
return 0;
}
}
function MODULE_preprocess_menu_link(&$variables)
. PS: I'll ask this as a separate question too.