63

\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)); $project_link = Link::fromTextAndUrl(t('Open Project'), $url); $project_link = $project_link->toRenderable(); // If you need some attributes. $project_link['...


58

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 because twig will cast this object as a string this will call the magic method toString() and will output the url as a string value. You only need to use url, ...


53

You need to use route:<nolink> to achieve this. Then it will validate and render without a link.


26

In order to create a menu item automatically this can be placed in a hook_update_N on the file mymodule.install and will run when the database is updated (/update.php): use Drupal\menu_link_content\Entity\MenuLinkContent; $items = array( '1' => 'Menuitem 1', '2' => 'Menuitem 2', '3' => 'Menuitem 3' ); foreach($items as $nid => $title) { ...


26

You can access the render array element directly, in your case: {{ node.field_my_link[0]['#url'] }}


24

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 these in a .module file, you may need to change this to t() 1. Basic Link to a node: $node = Node::load($nid); $build['node_link'] = $node->toLink()->...


23

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('This link was rendered') ]; Drupal supplies us with some helper methods to create urls and links to entities. $url = Node::load(NID)->toUrl('edit-form'); ...


23

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' => ['uri' => 'internal:/<front>'], 'title' => 'Front Page', 'menu_name' => 'admin', ]); $item->save(); Or in static configuration files: ...


20

D8 Editor Advanced link module Enhances the link Dialog in D8 CKEditor. Allows to define the following attributes: title class id target rel


20

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 through Drupal's path($name, $parameters, $options) function. {% if node.field_link.0.url.external %} <a href="{{ node.field_link.uri }}"> {{ node....


16

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 and each behaves a little bit different so here are the examples. With a route. This makes the most sense, you can just supply it in the options. Link::...


16

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.add title: 'Add My Entity' route_parameters: node_type: 'my_entity' options: query: destination: '/path/to/redirect' appears_on: - ...


15

You can achieve using below step: Go to Administration -> Structure -> Menus Click on 'Add links' in 'Management' menu row Add 'Menu link title' and 'Path' for menu item. Set 'Parent link' for the menu item, so it would display in correct position. Save the form Update: By default, Management menu be only dispaly to administrator or the user which has ...


15

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(), in my case I needed my_theme_preprocess_links__language_block() You need to take the twig file name and replace all - for _. my_theme.theme: function ...


15

Check the Menu Item Extras module.


15

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


14

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 = \Drupal::menuTree()->load('main', new \Drupal\Core\Menu\MenuTreeParameters()); foreach ($tree as $item) { $title = $item->link->getTitle(); $...


13

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(); $destination = $request->query->get('destination'); return new RedirectResponse($destination); When generating an url for the example mentioned in ...


12

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 MenuTreeParameters <?php /** * @file * Contains \Drupal\YOUR_MODULE\Plugin\Block\SectionsFooter. */ namespace Drupal\YOUR_MODULE\Plugin\Block; use ...


11

In Drupal 8 the contextual links markup is still printed via the {{ title_suffix }} variable (D7: print render($title_suffix);). block.html.twig (default): {% set classes = [ 'block', 'block-' ~ configuration.provider|clean_class, ] %} <div{{ attributes.addClass(classes) }}> {{ title_prefix }} {% if label %} <h2{{ ...


11

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 HOOK_entity_base_field_info(EntityTypeInterface $entity_type) { $fields = []; if ($entity_type->id() == 'menu_link_content') { $fields['FIELD_NAME'] = ...


11

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 "Log in me here please" and "Get me out of there" links with /user/login and /user/logout paths relatively. This is better because unlike the system "Log out" ...


10

This is a solution I found, but it isn't really handy to use... I really want a better solution, like something directly from twig templates. function template_preprocess_field(&$variables, $hook) { $element = $variables['element']; if ($element['#name'] == 'field_c_button_link') { $variables['items'][0]['content']['#options']['attributes']['...


10

I had to do this on a Link Field in a Block. This worked well: {{ content.field_my_link[0]['#url']|render }}


10

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 link is external or not (for my case it was for the target), you can use that: node.field_link.0.url.external Example: <a href="{{ node.field_link.0.url }}" ...


10

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.autocomplete_matcher: class: Drupal\Core\Entity\EntityAutocompleteMatcher arguments: ['@plugin.manager.entity_reference_selection'] In your custom module, add a class ...


10

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 accessed from a twig template. Among the many advantages of having full control of the menu's render array in a twig template is the ability to customize the ...


9

Figured this out with the help of an article! http://kristiankaa.dk/article/drupal8-region-specific-menu-theme-hook-suggestion // Add a region variable to a block. // http://kristiankaa.dk/article/drupal8-region-specific-menu-theme-hook-suggestion function THEMENAME_preprocess_block(&$variables) { if (isset($variables["elements"]["#id"])) { ...


9

I opened a drupal.org bug report on 20 december 2016 for this exact same request. The patch passes all tests and is now in the "Needs review" stage. Feel free to test the patch and mark the issue as RTBC.


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