I use Drupal 8.
I would simply want to add icons to one of the menus with a <span>
.
Have you runways?
I use Drupal 8.
I would simply want to add icons to one of the menus with a <span>
.
Have you runways?
This is how I'm doing it for dropdown operations(Font Awesome icons).
/**
* Implements hook_preprocess_links().
*/
function HOOK_preprocess_links(array &$variables) {
if (stripos($variables['theme_hook_original'], 'links__dropbutton__operations') === 0) {
$variables['attributes']['class'][] = 'menu';
$links = &$variables['links'];
foreach ($links AS $key => &$link) {
switch (Html::getClass($key)) {
case 'view';
case 'canonical':
$icon = 'eye';
break;
case 'edit':
$icon = 'pencil';
break;
case 'delete':
$icon = 'trash';
break;
case 'clone':
$icon = 'clone';
break;
case 'add':
case 'create':
$icon = 'plus';
break;
case 'manage-fields':
$icon = 'list';
break;
case 'manage-form-display':
$icon = 'server';
break;
case 'manage-display':
$icon = 'th';
break;
case 'devel':
$icon = 'code';
break;
case 'pay':
$icon = 'money';
break;
case 'enable':
$icon = 'toggle-off';
break;
case 'disable':
$icon = 'toggle-on';
break;
default:
$icon = 'question';
break;
}
$link['link']['#title'] = [
'#type' => 'inline_template',
'#template' => '<i class="fa fa-{{ icon }}" title="{{ title }}"></i><span class="visually-hidden">{{ title }}</span>',
'#context' => [
'title' => $link['link']['#title'],
'icon' => $icon
]
];
}
}
}
if (stripos($variables['theme_hook_original'], 'menu__account') === 0) {
or if ($variables['theme_hook_original'] == 'menu__account') {
but doesn't work...
Commented
Feb 1, 2016 at 14:38
There is a new easy alternative to display icons in Menu Items in Drupal 8:
The Menu Manipulator module now provides an option for you to create a list of available icons that editors can select when editing menu links.
The icon's ID is then passed to Twig so you can theme/render/display it however you want (e.g. an icon font such as FontAwesome, a custom Twig file, as a background image in CSS ...etc).
Example yourtheme/templates/menus/menu--main.html.twig
:
[...]
{% for item in items %}
<li{{ item.attributes.addClass('menu-item', 'column') }}>
{% if item.icon %}
{# Assuming you use so type of icon font #}
<span class="fa fa-{{item.icon}}"></span>
{# Assuming your icons are callable as Twig file. #}
{% include '@yourtheme/icons/icon.html.twig' with { icon: item.icon } %}
{% endif %}
{{ link(item.title, item.url) }}
{% if item.below %}
{{ menus.menu_links(item.below, attributes, menu_level + 1) }}
{% endif %}
</li>
{% endfor %}
[...]
You can check how the Menu Item Extras add the body field or you can use the body field abd insert image with the CKEditor.