You are here

function menu_manipulator_preprocess_menu in Menu Manipulator 8

Same name and namespace in other branches
  1. 8.2 menu_manipulator.module \menu_manipulator_preprocess_menu()
  2. 3.0.x menu_manipulator.module \menu_manipulator_preprocess_menu()
  3. 2.0.x menu_manipulator.module \menu_manipulator_preprocess_menu()

Implements theme_preprocess_menu().

File

./menu_manipulator.module, line 92
Contains menu_manipulator.module.

Code

function menu_manipulator_preprocess_menu(&$variables, $hook) {
  $config = \Drupal::config('menu_manipulator.settings');
  if ($config
    ->get('preprocess_menus_title')) {

    // Populate menu title variable for Twig.
    if (isset($variables['menu_name'])) {
      $menu = \Drupal::service('entity_type.manager')
        ->getStorage('menu')
        ->load($variables['menu_name']);
      if (NULL != $menu) {
        $variables['menu_title'] = [
          '#markup' => \Drupal::translation()
            ->translate('@label', [
            '@label' => $menu
              ->label(),
          ]),
        ];
      }
    }
  }
  if ($config
    ->get('preprocess_menus_language')) {
    if (isset($variables['menu_name'])) {

      // Automatically filter menu by language.
      $do_filter = TRUE;

      // Check if this menu has to be filtered.
      if (is_array($config
        ->get('preprocess_menus_language_list'))) {
        $do_filter = array_intersect([
          $variables['menu_name'],
        ], $config
          ->get('preprocess_menus_language_list'));
      }

      // Actually filter menu by language.
      if ($do_filter) {
        $menu_tree_translated = menu_manipulator_get_multilingual_menu($variables['menu_name']);
        $variables['items'] = isset($menu_tree_translated['#items']) ? $menu_tree_translated['#items'] : [];
      }
    }
  }
  if ($config
    ->get('preprocess_menus_icon')) {
    if (isset($variables['menu_name'])) {

      // Do not process icons variable by default.
      $do_filter = FALSE;

      // Check if this menu is selected has been selected by user.
      if (is_array($config
        ->get('preprocess_menus_icon_list'))) {
        $do_filter = array_intersect([
          $variables['menu_name'],
        ], $config
          ->get('preprocess_menus_icon_list'));
      }

      // Filter menu to display icons.
      if ($do_filter) {
        foreach ($variables['items'] as $key => $item) {
          $item_options = $item['original_link']
            ->getOptions();
          if (isset($item_options['icon']) && ($icon = $item_options['icon'])) {
            $variables['items'][$key]['icon'] = $icon;
          }
        }
      }
    }
  }
}