Overriding output using preprocess theme functions

Created on 9 September 2020, over 4 years ago
Updated 5 July 2024, 7 months ago

Problem/Motivation

I am trying to change the html output of the dropdown sub menu, and in particular the output of the link itself. I would like to output the link "description" text BEFORE the menu item. Using the nterface you can output it after the text and within the tag but it doesnt seem possible to change that.

There are other issues where people seem to have done similar things by overriding the preprocess theme functions, but I have not been able to get this to work.

So please can someone confirm how you do this or if it is not possible.

Steps to reproduce

I have tried inserting the entire theme function template_preprocess_superfish_menu_items from superfish.theme.inc into my theme file, and although the menu does display it stops rendering properly, only the top level shows and no children are being added.

I have also tried adjusting the function to see if it works to simply insert a variable for description without the rest of the function but that does not work, resulting in errors such as "Error: Cannot use object of type Drupal\\Core\\Menu\\MenuLinkTreeElement as array" and "Error: Call to a member function getDescription() on null" depending on how I have tried to adjust the function.

Proposed resolution

Ideally if it is possible to override these functions some documentation or examples of working theme functions would really help.

I have tried using the issue below as an example but I am not trying to use menu extra items but just to be able to add the description as a variable that can then be used in the template.

Feature request
Status

Active

Version

1.4

Component

Documentation

Created by

🇬🇧United Kingdom gsquirrel London

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇬🇧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]);
      }
    }
    
Production build 0.71.5 2024