Problem/Motivation
When trying to generate a link to a Drupal page in a template, it would be preferable to use the route name (the immutable machine name) rather than a system path that may be altered if the route path pattern is altered.
Proposed resolution
TBD
Remaining tasks
TBD
User interface changes
None
API changes
None
Related Issues
Follow-up from #2073811-59: Add a url generator twig extension (comment #59).
Original report by @Fabianx
Links are complicated. A link function could also use an OOP style approach - or just return variables or even just a link render array.
Example:
{{ link() }} but also {% set my_link = link(node.title, 'node.view', { 'nid': node.nid }) %} <a href="{{ link.url }}" {{ link.attributes }}>{{ link.content }}</a>
But that is another discussion that needs to be done in a wider scope and also discuss how to support render arrays existing and not introduced here in the change of url() in twig to support routes.
Comments
Comment #1
cosmicdreams CreditAttribution: cosmicdreams commentedThe fact that your example shows
mean it has a code smell. Anytime I see an empty argument I think it has a bad code smell. Why not structure your arguments so that the unneeded argument is last and therefore could be omitted?
Comment #2
markhalliwell@cosmicdreams, I updated the issue summary to reflect that this issue was simply a follow up to a comment posted by @Fabianx. I doubt we'll be using that syntax at all. It will likely follow a similar pattern using routes (like the follow-up issue). The blanks argument is actually for text of the link (which is 99% used). In this case, I think @Fabianx was just giving an example of what is possible with setting variables via Twig and not needing to provide the text. In all actuality, it will probably be something similar to:
Comment #2.0
markhalliwellUpdated issue summary.
Comment #2.1
markhalliwellUpdated issue summary.
Comment #3
star-szr#2073811: Add a url generator twig extension was not a major task so I don't think this should be. Can we outline the main benefits/use cases and the plan in the issue summary? The related issue talks about active class handling for example and I assume this would be an extension for l(). Thanks!
Comment #4
dawehnerWell, one usecase is consistency. If you work example need the active class on there, it is impossible to get it set automatically.
Comment #5
joelpittet@dawehner Can/could the urlGenerator object provide that active route/path state?
Pardon my ignorance, I've not got a chance to dig into the routing/urlGenerator internals too deep yet.
Comment #6
dawehner@joelpittet
Yeah links are tricky, sadly.
There are a couple of problems with just using direct html, as you suggested
hook_link_alter
Comment #7
joelpittetOuch yeah they are tricky now eh? JS to set the active class:S That sounds like a last ditch resort to deal with caching... hmm.
Maybe we should talk to Wim and see where that is going, if he knows?
If a linkGenerator is the only way we can ATM deal with active links in templates... that will need to go in but I'd much rather see a smart Url object could let the template know of it's state I think.
Comment #8
dawehnerWhat we could also do is to use the new link object we have and put the needed information into that.
Comment #9
Wim LeersIt's all explained in https://www.drupal.org/node/2167077, specifically in the "Solution" part, second point.
In short: you must use
LinkGenerator
. AFAIK/IIRCUrl
objects are simple value objects, so this definitely would not be great:Why can't this "link generator twig extension"… use
LinkGenerator
under the hood? The names match perfectly even!Comment #10
Wim LeersComment #11
dawehnerNote: In order to solve #1777332: Replace theme_menu_link() and menu-tree.html.twig with a single Twig template I introduced such a link generator.
Comment #12
pivica CreditAttribution: pivica commentedNote that #1777332: Replace theme_menu_link() and menu-tree.html.twig with a single Twig template introduced very basic link implementation, meaning you can only pass path text and url but not the rest stuff like attributes. This link implementation is already used in menu.html.twig which means that currently it is not possible to inject additional classes or any other attributes while rendering menus with twig.
UPDATE: just found separate issue for attribute problem #2342745: Allow Twig link function to pass in HTML attributes.
Comment #13
joelpittetGlad you found it:)
Comment #14
Crell CreditAttribution: Crell at Palantir.net commentedSo um, sounds like this is done, then, no? You *can* add links from a template, right...? We can close this?
Comment #15
joelpittetYes this is a duplicate thanks.
Comment #16
joelpittetDuplicate of... #1777332: Replace theme_menu_link() and menu-tree.html.twig with a single Twig template where it snuck in;)