Member-only story
How to Update a Menu Item Link In Drupal if You Know The ID?
Easily change a link automatically
There are occasions when you need to update a link in a menu, but you want to avoid doing it manually, just in case you forget to do it by the time your code gets to the live server.
I’ve seen a lot of examples of creating new menu items programmatically but only a few about editing existing menu items.
This is how you can do it. Replace 3925 with the id of the menu item you want to edit and ”internal:/toolkits” with your url (use “internal:/” just for urls within your site).
Add this code in the *.install file of your custom module:
use Drupal\menu_link_content\Entity\MenuLinkContent;/**
* Update Toolkits menu item link.
* @param $sandbox
* @throws \Drupal\Core\Entity\EntityStorageException
*/
function custom_module_update_8001(&$sandbox) {
$menu_link = MenuLinkContent::load(3925);
$menu_link->link = array('uri' => 'internal:/toolkits');
$menu_link->save();
}
👉 Find out more about me here: https://sergioguardiola.net 🔥