function NavigationTestHooks::navigationMenuLinkTreeAlter

Implements hook_navigation_menu_link_tree_alter().

Attributes

#[Hook('navigation_menu_link_tree_alter')]

File

core/modules/navigation/tests/navigation_test/src/Hook/NavigationTestHooks.php, line 90

Class

NavigationTestHooks
Hooks implementations for navigation_test module.

Namespace

Drupal\navigation_test\Hook

Code

public function navigationMenuLinkTreeAlter(array &$tree) : void {
  foreach ($tree as $key => $item) {
    // Skip elements where menu is not the 'admin' one.
    $menu_name = $item->link
      ->getMenuName();
    // Removes all items from menu1.
    if ($menu_name == 'menu1') {
      unset($tree[$key]);
    }
    // Updates title for items in menu2
    if ($menu_name == 'menu2') {
      $item->link
        ->updateLink([
        'title' => 'New Link Title',
      ], FALSE);
    }
  }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.