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.

Comments

s_leu created an issue. See original summary.

s_leu’s picture

Status: Active » Needs review
StatusFileSize
new3.17 KB

Adding 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.

  • AndersNielsen committed 1abe18e on 8.x-1.x authored by s_leu
    Issue #2913433 by s_leu: Support nested menu links and fix pre-...
AndersNielsen’s picture

Status: Needs review » Fixed

Had to reroll after fixing other issues, but works like a charm. Thank you for the effort, pushed to develop :)

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.