5

How to allow html tag in menu in Drupal 8. For example if use home @reg used in menu title it display the html tag.

2 Answers 2

8

Following solution works:

use Drupal\Core\Render\Markup; /** * Implements hook_preprocess_HOOK() */ function YOURTHEME_preprocess_menu(&$variables){ foreach($variables['items'] as &$link){ $link['title'] = Markup::create($link['title']); } } 

Source: Allow html into menu item names - Solution for Drupal 8

1

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; } } 
2
  • blogs tend to go away, but stackexchange sticks around. Could you copy the best excerpts of the ideas of that blog here? Commented Jan 6, 2020 at 17:54
  • 1
    Ok, pasted it in the original answer. Commented Jan 7, 2020 at 7:58

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.