- 🇬🇧United Kingdom mistergroove
Old issue but putting it here to see if there's a better solution to this...
This is what I did - in order to add title based classes. It's not the most elegant or efficient solution tho as I had to convert the FormattableMarkup to a string to get the title - and then I could 'clean' it and add it to the menuitem attribute classes.
I also wanted to wrap the menu item like '@text' so I had to replace the FormatableMarkup.
I couldn't find a nice way to do this as the twig template doesn't let you wrap the links..
function mytheme_preprocess_superfish_menu_items(array &$variables) { foreach($variables['menu_items'] as &$menuitem) { // Add class based on cleaned title $title = (string) $menuitem['link']['#title']; $itemclass = 'item-title-' . Html::cleanCssIdentifier(strtolower($title)); $menuitem['attributes']->addClass($itemclass); // Wrap title in span tag $menuitem['link']['#title'] = new FormattableMarkup('<span>@text</span>', ['@text' => $title]); } }