Skip to content

Instantly share code, notes, and snippets.

@luzzardi
Last active February 16, 2024 17:28
Show Gist options
  • Save luzzardi/d326cbc7aedc3ba1c76d46c824e46570 to your computer and use it in GitHub Desktop.
Save luzzardi/d326cbc7aedc3ba1c76d46c824e46570 to your computer and use it in GitHub Desktop.
Preprocess page and get menu item for the current node and load all menu parent three
<?php
/**
* @file
* Primary module hooks for module.
*
* @DCG
* This file is no longer required in Drupal 8.
* @see https://www.drupal.org/node/2217931
*/
use Drupal\Core\Menu\MenuTreeParameters;
/**
* Implements hook_preprocess_HOOK().
*/
function <module_name>_preprocess_page(&$variables) {
if(!empty($variables['node'])) {
$node = $variables['node'];
// Get menu three service.
$menu_tree = \Drupal::menuTree();
// Get menu link manager service.
$menu_link_manager = \Drupal::service('plugin.manager.menu.link');
// Load menu items for the current node.
$result = $menu_link_manager->loadLinksByRoute('entity.node.canonical', array('node' => $node->id()));
/** @var \Drupal\menu_link_content\Plugin\Menu\MenuLinkContent $result */
$result = current($result);
dump($result->getMenuName());
$menu_tree = $menu_tree->load($result->getMenuName(), new MenuTreeParameters());
dump($menu_tree);
dd($result);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment