0

I am trying to add an internal link to admin toolbar but I am not able to achieve it.

According to documentation we add an internal link in *.links.menu.yml in the below manner but the link is not being appeared in the toolbar.

hello_world.admin2:
  title: 'Hello module settings'
  description: 'example of how to make an admin settings page link'
  parent: system.admin_config_development
  url: internal:/node/123

I tried the following variants as well but none works. url: 'internal:/node/123' or uri: internal:/node/123 or uri: 'internal:/node/123' .

But the following works url: http://localhost/node/123 .

Can anyone suggest me a way to create a menu link in *.links.menu.yml file.

Use case : I want to provide easily accessible link to user to access one particular page. It is not a route. I am aware of other way to create menu link by using MenuLinkContent::create method. But trying to figure out why above method is not working.

1

2 Answers 2

3

I don't know thy this doesn't work (it is a valid input for Drupal\Core\Url::fromUri), but /node/123 actually has a route:

hello_world.admin2:
  route_name: entity.node.canonical
  route_parameters:
    node: 123

Even though they are valid inputs, it is generally a good idea to avoid internal: URLs if you can use something better. "Something better" is either a route name (in the context of links.yml files) or an entity scheme entity:node/123 (in the context of storing links in the DB). AFAIK internal: was meant for local unrouted URLs like e.g. internal:/robots.txt

1
  • Thank you very much for the hint. It works. Bit i am still wondering why the method in question is not working. I will wait couple of days to check if anyone can provide any additional information on why it is not working. Or else I will mark your answer as accepted answer. Nov 12, 2021 at 16:17
0

@miststudent2011 I think the idea you're missing is that a menu link offers either a route or a url.

From the Drupal 10.1 Menu API intro page:

links point to routes or URLs.

From the Drupal 10.1 documentation for function hook_menu_links_discovered_alter:

route_name: (optional) The route name to be used to build the path. Either the route_name or url element must be provided.

route_parameters: (optional) The route parameters to build the path.

url: (optional) If you have an external link use this element instead of providing route_name.

Put simply, use route_name for internal links. Use url for external links. Not both!

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.