68 votes
Accepted

How do I create a link?

\Drupal::l is deprecated. Maybe this case will be useful for somebody use Drupal\Core\Url; use Drupal\Core\Link; $url = Url::fromRoute('entity.node.edit_form', array('node' => NID)); $...
Eugene's user avatar
  • 2,429
68 votes
Accepted

How to get the valid URL of a Link field from within a Twig template?

Your PHP from your latest edit would translate into twig like this: {{ node.field_my_link.0.url }} This works like this, 0 returns the first item of the field item list, url gets the url object and ...
4uk4's user avatar
  • 99.2k
56 votes
Accepted

Non-Clickable Menu Items

You need to use route:<nolink> to achieve this. Then it will validate and render without a link.
Kevin's user avatar
  • 27.8k
37 votes

How do I create a link?

Here are some examples of creating links in Drupal 8. Note that $this->t('some text') is available within blocks that extend BlockBase. If you copy these to another class that doesn't have it or use ...
oknate's user avatar
  • 14.2k
30 votes

How to get the valid URL of a Link field from within a Twig template?

You can access the render array element directly, in your case: {{ node.field_my_link[0]['#url'] }}
Tim's user avatar
  • 1,387
28 votes
Accepted

How do I create menu links programmatically?

In order to create a menu item automatically this can be placed in a hook_update_N() implementation in the .install file; it will run when the database is updated. /** * Put the following line on the ...
skorzh's user avatar
  • 1,474
26 votes
Accepted

How can I add items to the Admin Toolbar?

You can add items to the admin toolbar in two ways: As content: In ui /admin/structure/menu/manage/admin or in code: $item = \Drupal\menu_link_content\Entity\MenuLinkContent::create([ 'link' =&...
4uk4's user avatar
  • 99.2k
24 votes

How to get the valid URL of a Link field from within a Twig template?

You have to cover both cases: external and internal URLs. First let's check if the URL is external and simply print its *.uri. Else if it's internal we have to wire its route name and parameters ...
leymannx's user avatar
  • 18k
23 votes

How do I create a link?

Another option is to create a link in a render array $url = Url::fromRoute('entity.node.edit_form', array('node' => NID)); $link = [ '#type' => 'link', '#url' => $url, '#title' => t(...
Eyal's user avatar
  • 3,246
21 votes
Accepted

How to set target="_blank" for links in CKEditor

D8 Editor Advanced link module Enhances the link Dialog in D8 CKEditor. Allows to define the following attributes: title class id target rel
No Sssweat's user avatar
  • 31.1k
19 votes

How to get the valid URL of a Link field from within a Twig template?

I had to do this on a Link Field in a Block. This worked well: {{ content.field_my_link[0]['#url']|render }}
cbovard's user avatar
  • 191
19 votes
Accepted

Drupal 8 Permission to see Structure > Menu

You would have to grant the following permissions to the role: Required Use the toolbar Administer menus and menu items Use the administration pages and help Optional View the administration theme
mvdgun's user avatar
  • 1,910
18 votes

How to turn a menu tree into an associative array?

You can load up the menu tree with MenuLinkTree::load, which will give you an array of menu links to work with. From there it's just a matter of looping through and getting what you need, e.g. $tree =...
Clive's user avatar
  • 167k
17 votes
Accepted

Can we add the destination query parameter to a link from yml configuration?

Yes, you can add the destination query directly in the yml file. @see : \Drupal\Core\Menu\LocalActionDefault::getOptions ie: mymodule.links.action.yml my_module.my_entity.add: route_name: node....
jeanfei's user avatar
  • 186
17 votes
Accepted

How do I alter a language item link?

Ok I found 2 ways to do that. 1. In a custom theme You can alter variables in your my_theme.theme file. You need to figure out the name of the function you need. ex: my_theme_preprocess_twig_file(), ...
Guillaume Bois's user avatar
16 votes

How do I create a link?

The ability to set attributes seems lost or convoluted in the previous examples because the functionality is just not obvious. There are two ways to create links depending on if it has a route or not ...
NecLimDul's user avatar
  • 189
16 votes

How to add fields to menu items

Check the Menu Item Extras module.
Mykhailo Gurei's user avatar
15 votes

How to get the valid URL of a Link field from within a Twig template?

you can also use something like the following, it all depends of the type of object you are extracting and the field name, In my case it was a paragraph with a field named field_link and the following ...
Israel Morales's user avatar
14 votes
Accepted

How to display a block with menu child items programmatically?

I found the answer based in this question: How to turn a menu tree into an associative array? You can use each array element as a item of MenuLinkInterface To see others parameters check the class ...
Adrian Cid Almaguer's user avatar
13 votes

Can we add the destination query parameter to a link from yml configuration?

The query string is not part of the route. The controller fetches the query parameters from the webserver request and does the processing, in this case returns a redirect: $request = \Drupal::request(...
4uk4's user avatar
  • 99.2k
13 votes

How to add a Login / Logout link in Main navigation menu?

We don't need to depend on Bootstrap theme or our programming skills to move system "Log out" link on any other menu. Drupal allows us to make it easily. Additionally we can just create two custom "...
drugan's user avatar
  • 466
13 votes
Accepted

Render menu in twig template

Simplify Menu sounds like what you'd want. The simplify_menu module uses a TwigExtension to gain access to Drupal's main menu's (or any other menu for that matter), render array so it can be ...
Kevin's user avatar
  • 27.8k
12 votes

How do I create menu links programmatically?

If you would like to create module-defined menu links, add something like this example to your custom_module.links.menu.yml file: custom_module.admin_item_1: title: 'New Admin Item 1' parent: ...
colan's user avatar
  • 2,169
12 votes

How to add fields to menu items

You can use hook_entity_base_field_info to add an additional field to the menu_link_content entity with the following code: /** * Implements hook_entity_base_field_info(). */ function ...
Decipher's user avatar
  • 3,722
12 votes
Accepted

Get menu link item from MenuLinkTreeElement

You can get the menu link content entity by accessing the uuid with the method getDerivativeId() of the plugin: if ($link instanceof \Drupal\menu_link_content\Plugin\Menu\MenuLinkContent) { $uuid = ...
4uk4's user avatar
  • 99.2k
11 votes

How to get the valid URL of a Link field from within a Twig template?

I know it's an old post but with the last version of Drupal 8 (8.6) you can directly display the url for both version (external and internal): node.field_link.0.url And if you need to know if the ...
AnTSaSk's user avatar
  • 211
10 votes

How to turn a menu tree into an associative array?

I'm quite late, but maybe helps someone looking for answers, here's my solution for generating recursive array from menu items. private function generateSubMenuTree(&$output, &$input, $...
Siim's user avatar
  • 346
10 votes
Accepted

How do I alter the title of a menu link?

I suggest implementing a custom menu link plugin. The code below assumes your module name is example. <?php namespace Drupal\example\Plugin\Menu; use Drupal\Core\Database\Connection; use Drupal\...
ya.teck's user avatar
  • 5,604
10 votes

How to display more than 10 items in link widget autocomplete?

If you can live with overriding all of the autocomplete limits, you can override a core service in Drupal 8; The service you need to override is here in core.services.yml: entity....
oknate's user avatar
  • 14.2k
10 votes

Render menu in twig template

For those who are still looking for an alternative approach, I suggest using Twig Tweak module as a more stable and more global use case, check out its cheat sheet For rendering the menu via twig ...
Sohail's user avatar
  • 1,544

Only top scored, non community-wiki answers of a minimum length are eligible