Menu title defined by class in plugin

Created on 20 July 2019, almost 6 years ago
Updated 19 April 2025, 14 days ago

If the menu is defined by a menuLink class, superfish menu do not accept markup array as title input

Ex.

Menu yml:

my_module_tool:
  weight: -10
  title: 'title'
  description: 'description'
  menu_name: tools
  class: Drupal\my_module\Plugin\Menu\MyModuleMenuLink

In MyModuleMenuLink:

public function getTitle() {

 //ok with superfish
 return $this->t('Modified title');

 //superfish will not return string in this case while it is returned with Drupal core tools menu
 return [
    '#markup' => $this->t('Modified title'),
    '#attached' => ['library' => ['my_module/library'],],
 ];

}
πŸ› Bug report
Status

Closed: won't fix

Version

1.0

Component

Code

Created by

πŸ‡ΈπŸ‡¬Singapore adominique

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.

  • πŸ‡§πŸ‡ͺBelgium lobsterr

    After my investigation of many classes which implement MenuLinkInterface

    1) MenuLinkInterface expects string

      /**
       * Returns the localized title to be shown for this link.
       *
       * @return string
       *   The title of the menu link.
       */
      public function getTitle();
    

    2) by default menu links are passed through DefaultMenuLinkTreeManipulators class:

    $new_tree[(50000 + $instance->getWeight()) . ' ' . $instance->getTitle() . ' ' . $instance->getPluginId()] = $tree[$key];

    As you can see in the core nothing special is done with getTitle method result and just a simple string is expected and not array. As a result I get warining:

    Warning: Array to string conversion in Drupal\Core\Menu\DefaultMenuLinkTreeManipulators->generateIndexAndSort() (line 245 
    

    From here I can conclude: yes, it works! because conversion of an array to string, but definitely it is not what expected. It is not a bug.

Production build 0.71.5 2024