62

\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['...


53

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, ...


50

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


25

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) { ...


23

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


22

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'); ...


21

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()->...


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 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: ...


17

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

PHP typecasting pop quiz time! (Full disclosure: I didn't win in our office). As pointed out already, this is the code in l() that we're concerned with: if (isset($options['fragment']) && $options['fragment'] !== '') { $options['fragment'] = '#' . $options['fragment']; } So here's the question: What value casts to an empty string but passes the ...


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

Check the Menu Item Extras module.


14

You can do it by defining a page in a custom module. Create a page for the category "Page not found" using hook_menu(). function MODULE_menu() { $items['page-not-found'] = array( 'title' => '', 'page callback' => 'MODULE_page_not_found', 'access callback' => TRUE, ); return $items; } function MODULE_page_not_found() { ...


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 ...


13

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 ...


13

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(); $...


12

If you could not use clean "#" with fragment, I would suggest you to use <a href="javascript:">link</a>. When adding "javascript:" or "javascript:void(0)" to href attribute, the <a> tag will do nothing. Then you can bind "onclick" to the link for your ajax purpose. You can implement this using l() function as below: l('link', '...


11

There is a module to make this work 404 Navigation On 404 Not Found error pages, Drupal will skip rendering of all the navigation links of your website for performance reasons*. Some of these include: The "Primary links" block and any other menu links block. The Primary links and Secondary links of your theme. * Unless you have configured a "...


11

You want to implement hook_translated_menu_link_alter(). This hook is not called for all menu items. According to the API documentation: It is only invoked if $item['options']['alter'] has been set to a non-empty value (e.g., TRUE). This flag should be set using hook_menu_link_alter(). Here's an example: function MODULE_menu_link_alter(&$link) {...


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

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 ...


10

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


10

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

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

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


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