2

When trying to update an existing menu link URL/ URI with updateDefinition() -

https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Menu%21MenuLinkManagerInterface.php/function/MenuLinkManagerInterface%3A%3AupdateDefinition/8.2.x

It's noticed the url is not getting updated. Other key such as 'title', 'menu_name', 'description', 'enabled' etc gets updated.

This is expected as these are the keys that are allowed to override. I'm trying to override a url or route of an existing menu item. Is there any other way I can achieve this? Below is my current implementation -

$menu_name = 'custom';
$menu_tree = \Drupal::menuTree();
$parameters = $menu_tree->getCurrentRouteMenuTreeParameters($menu_name);

$tree = $menu_tree->load($menu_name, $parameters);
$manipulators = array(
  array('callable' => 'menu.default_tree_manipulators:checkAccess'),
  array('callable' => 'menu.default_tree_manipulators:generateIndexAndSort'),
);
$tree = $menu_tree->transform($tree, $manipulators);

foreach ($tree as $item) {
  //update existing menu item.
  $item->link->updateLink(
      [
        'enabled'=> 0,
        'title'=>'new name',
        'link' => ['uri' => 'internal:/node/1000'],  //Need to update to a new internal url.
        'menu_name' => 'custom',
      ],
      true
   );

}

I'm open for any suggestions/ ideas. Thanks!

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.