
Drupal advance topic: programmatically change menu links for enhanced routing. A code example shows how to customize a menu item according to user contexts.
This capability allows developers to create dynamic menus that adapt to various user roles, contexts, and conditions, thereby improving the overall navigation experience on a site.
In this article, we’ll explore how to programmatically modify menu links in Drupal 11, ensuring you can build a more flexible navigation structure.
Before diving into the programmatically change menu links
It is important to understand menu items in #Drupal 11 before going to the solution. Menu is an entity now in Drupal core, that means it can be fieldable, rendered and highly flexible to construct as per the context.
What are menus in Drupal
Maintaining the readability of your site content, a menu helps you arrange it in a user-friendly, simple-to-navigate manner. The menu can contain one or several menu links. These links are anchor links that can redirect the user to different pages of the website.
How to create a menu in Drupal 11
Log in as an administrator and go to /admin/structure/menu. Then, in the top right corner, click the "+Add menu" button to make a menu.

Once you click the button, it will open a form to fill the details.

Fill the required details and hit the save button. After that you can add menu links to display at specific region.
To understand the complete workflow, first we will create a menu item on the menu that we had created before. For example I have created a menu called "Main Menu English", then I will go the the menu listing page and click on Edit Menu action button.
Here we can add a link to the menu by clicking on the +Add link button as show below.

Next step is to add the menu link using below form.

We have created a menu link User Profile and this item will open /user/2 URL. Make a note in the URL user/2, 2 is the user id that can be different for different users. We will programmatically change menu links that will dynamically change this id to a particular user id.
Step-by-Step Guide to Programmatically Change Menu Links
if you want to create menu links via code that can also be done so that you can automate this process and create the same links in production environment that you have created manually to your local environment. Here is the detail blog post on How to make non clickable menu item programmatically in Drupal
Back to our example, we have added a link via UI that will open /user/2 page and I want to change it dynamically to open /user/4 in some situations.
Step 1:
Create a custom module broute.
# web/modules/custom/broute/broute.info.yml
name: Bhimmu Route
description: 'Custom module for routing'
type: module
core_version_requirement: ^11
package: Custom
Step 2:
Implement a hook hook_link_alter to alter the route parameter. We will implement the Hook class as Drupal 11 supports hook implementation in OO way
# web/modules/custom/broute/src/Hook/LinkHook.php
<?php
namespace Drupal\broute\Hook;
use Drupal\Core\Hook\Attribute\Hook;
class LinkHook {
#[Hook('link_alter')]
public function linkAlterCustom(&$variables) {
/** @var \Drupal\Core\Render\Markup $linkText */
$linkText = $variables['text'];
if (str_contains($linkText->__toString(), 'User Profile')) {
$variables['url']->setRouteParameter('user', 4);
}
}
}
First we are matching that the link we are trying to change exists or not. If that exist then we can change the route parameter user from value to 4.
Now, we will be redirected to user/4 profile instead of user/2
Conclusion
To change/alter a link created manually or programmatically, you can change the text, url, attributes etc. using the hook hook_link_alter.
- Create a custom module
- Implement a hook_link_alter
- Search for the targeted link
- Alter the parameter based on your requirement.