Hi there,

is there a way to make Drupal not filter html special characters from menu names? I need to put a non breaking space in one of menu items, yet I don't want to create a static template-hardcoded menu. Unfortunately Drupal keeps translating the ' ', so item names look like this "my humble page 1" :(

I'd be gratefull for some help :)

Comments

pielgrzym’s picture

Drupal keeps translating the "& nbsp ;" :)

litwol’s picture

Hello.

i am no expert but while digging through the handbook and API i managed to create custom menu for my site.

please refer to the following discussion: http://drupal.org/node/107821

there is a lot to read through, but i can assure you that you can ignore most of it. pay close attention to the menu_item.tpl.php and template.php that calls that menu_item.

by using custom menu_item you can define ANY kind of menu you wish.

if you have further questions i'd be happy to answer them.

Sometimes something interesting appears on http://litwol.com

darekirl’s picture

Not working. Is it possible in Drupal 7?

ask’s picture

You should take a look at Menu HTML module: http://drupal.org/project/menu_html

jdln’s picture

Ive found menu html to be quite buggy. Are their any other module solutions?

sill’s picture

I had no trouble with the menu html module. Although, I was just implementing a simple line break and then making it pretty with CSS.

xalexas’s picture

It's super simple. Add this to your template.php:

function YOURTHEMENAME_link($variables) {
	$variables['options']['html'] = TRUE;
  return '<a href="' . check_plain(url($variables['path'], $variables['options'])) . '"' . drupal_attributes($variables['options']['attributes']) . '>' . ($variables['options']['html'] ? $variables['text'] : check_plain($variables['text'])) . '</a>';
}

replace YOURTHEMENAME with your theme name.
This is Drupal 7 core function theme_link() and I just added option $variables['options']['html'] = TRUE; and that's it.

tevih’s picture

yup! thanks! this did it perfectly

vacho’s picture

PERFECT!! function with superfish too

rwilson0429’s picture

The function worked perfectly and no need to add yet another module. Adding this function to my template.php, I was able to put an html non-breaking space ( ) in my menu tabs titles. Thanks xalexas.

ReggieW

stragu’s picture

Thank you for that.
How can I extend it to also affect the links in book-navigation?
The links in the navigation menu (sidebar) show the formatted text, but not the ones that are provided by book-navigation.tpl.php (under the book page).

gargsuchi’s picture

mιχaliς’s picture

For Drupal 8 your link title must be a instance of MarkupInterface.

You can rewrite your menu link like this :


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']);
  }
}
vimalabhi89’s picture

Hi mixalis44,

I'm trying for the same in drupal8. Should this change be done in the customtheme.theme in the below function ?

This is how it looks :

function custom_theme_preprocess_menu(&$variables, $hook) {
  if ($hook == 'menu__main') { // We're doing that for main menu.
    // Get the current path.
    $current_path = \Drupal::request()->getRequestUri();
    $items = $variables['items'];
    foreach ($items as $key => $item) {
      // If path is current_path, set active to li.
      if ($item['url']->toString() == $current_path) {
      // Add active link.
      $variables['items'][$key]['attributes']['class'] = 'active';
      }
    }

  }
}

Adding your code here gives me an 500 internal server error. Appreciate any help on this.

adityaj’s picture

Your solution works like charm! 

alternativo’s picture

Hi Kaelfaz, could please explain me where do I have to insert the function YOUTHEME...?

thanks 

zeroplexer’s picture

I would like to allow html into a link-field. How should I customise the code?

danrod’s picture

What if you have sub menus (children) of primary menu links? This should work for this case:

/**
 * Implements hook_preprocess_HOOK()
 */
function hook_preprocess_menu__main(&$variables){
  foreach($variables['items'] as &$link){
    $link['title'] = Markup::create($link['title']);
    if (isset($link['below']) && count($link['below'] > 0)){
      foreach ($link['below'] as &$sublinks) {
        $sublinks['title'] = Markup::create($sublinks['title']);
      }
    }
  }
}
Shahnawaz_grazitti’s picture

 

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']);
  }
}

Replace your theme name with the YOURTHEME.
Hope it helps

someshver’s picture

If you have multi-level menu use this

/**
 * Implements hook_preprocess_HOOK()
 */
function theme_preprocess_menu(&$variables){
  if($variables['menu_name'] == "menu-name"){
    foreach($variables['items'] as &$link){
        $link['title'] = \Drupal\Core\Render\Markup::create($link['title']);
        if(!empty($link['below'])){
          foreach($link['below'] as &$clink){
            $clink['title'] = \Drupal\Core\Render\Markup::create($clink['title']);
          }
        }
    }
  }
}