Problem/Motivation
Currently the module allows selection of icons for nested menu links (links which are not on the top level. However, the icons on nested links are not picked up by the code at this point.
Besides this, there's problems in menu_svg_icons_preprocess_menu() which currently looks like this:
if ($icon_set_menu_conf = \Drupal::config('menu_svg_icons.icon_set_menu.' . $variables['menu_name'])) {
$icon_set = \Drupal::config('menu_svg_icons.icon_set.' . $icon_set_menu_conf->get('icon_set'));
if ($icon_set) {
foreach ($variables['items'] as $key => $item) {
$item_title = $item['title'];
$variables['items'][$key]['title'] = [
'#theme' => 'menu_svg_icons_link',
'#title' => $item_title,
'#icon' => $item['url']->getOption('icon') == 'no_icon' ? NULL : $item['url']->getOption('icon'),
'#icon_height' => $icon_set->get('icon_width') ? 'height: ' . $icon_set->get('icon_width') . 'px;' : '',
'#icon_width' => $icon_set->get('icon_height') ? ' width: ' . $icon_set->get('icon_height') . 'px;' : '',
'#placement' => $icon_set->get('placement'),
];
}
}
}
The conditions are well intended but don't work as they are now, because loading config will always return an object, regardless of whether there is actually a config entity available or not. So the conditions won't ever stop the contained block of code from being executed.
Proposed resolution
To support icons on all levels, a recursively called function is required. In order to fix the conditions in the pre-process hook, we can use the static load methods for entities instead of loading the entities via config. That will return null on fail and make the conditions work.
| Comment | File | Size | Author |
|---|---|---|---|
| #2 | support-icons-on-nested-links_2913433_2.patch | 3.17 KB | s_leu |
Comments
Comment #2
s_leu commentedAdding a patch that fixes the mentioned points. Currently added the function as a static method on the config entity class IconSet, as a service for just one function seemed like an overkill here.
Comment #4
AndersNielsen commentedHad to reroll after fixing other issues, but works like a charm. Thank you for the effort, pushed to develop :)