function menu_manipulator_preprocess_menu in Menu Manipulator 2.0.x
Same name and namespace in other branches
- 8.2 menu_manipulator.module \menu_manipulator_preprocess_menu()
- 8 menu_manipulator.module \menu_manipulator_preprocess_menu()
- 3.0.x menu_manipulator.module \menu_manipulator_preprocess_menu()
Implements theme_preprocess_menu().
File
- ./
menu_manipulator.module, line 93 - Contains menu_manipulator.module.
Code
function menu_manipulator_preprocess_menu(&$variables, $hook) {
if (!isset($variables['menu_name'])) {
return;
}
$config = \Drupal::config('menu_manipulator.settings');
$menu_name = $variables['menu_name'];
// Display menu title variable for Twig.
if ($config
->get('preprocess_menus_title')) {
$menu = \Drupal::service('entity_type.manager')
->getStorage('menu')
->load($menu_name);
$variables['menu_title'] = [
'#markup' => t('@label', [
'@label' => $menu
->label(),
]),
];
}
// Filter menu by language.
if ($config
->get('preprocess_menus_language')) {
$selected_menus = $config
->get('preprocess_menus_language_list') ?? [];
$selected_menus = array_filter($selected_menus, function ($v) {
return $v !== 0;
});
// Filter this menu by default.
if (empty($selected_menus)) {
$selected_menus[$menu_name] = $menu_name;
}
// Filter links now.
if (($selected_menus[$menu_name] ?? 0) !== 0) {
$manipulator = \Drupal::service('menu_manipulator.menu_tree_manipulators');
foreach (Element::children($variables['items']) as $i) {
if (($link = $variables['items'][$i]['original_link'] ?? NULL) instanceof MenuLinkBase) {
if (!$manipulator
->checkLinkAccess($link)) {
unset($variables['items'][$i]);
}
}
}
}
}
// Display icons.
if ($config
->get('preprocess_menus_icon')) {
$selected_menus = $config
->get('preprocess_menus_icon_list') ?? [];
$selected_menus = array_filter($selected_menus, function ($v) {
return $v !== 0;
});
if (($selected_menus[$menu_name] ?? 0) !== 0) {
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;
}
}
}
}
}