Problem/Motivation
Twig function link($text, $url, $attributes)
where $text
can be only plaintext there is no way of providing markup within the link within a Twig template.
Proposed resolution
When the incoming $text
value is Twig_Markup
object, cast it to TwigMarkupInterface
when passing it back to the render system to preserve markup.
User interface changes
N/A
API changes
N/A
Original Issue Summary
Twig is suppose to provide a separation between front/back end logic but with menus this stops at the menu links. Because they utilize a Twig function link($text, $url, $attributes)
where $text
can be only plaintext I have no way of modifying markup within the link through Twig template logic.
1) Is there a work around? Specifically I need to use Twig logic to insert some markup within the <a>
tag into certain links other than just the plaintext title.
2) Longer term we really need to work getting this and other markup more easily accessible on the Twig templates themselves. Drupal has a long history of not being that friendly for front-end developers and while 8.x has come a long way by using Twig there seems to still be a lot of unnecessary complexity when it comes to making simple markup changes thus as this.
Comment | File | Size | Author |
---|---|---|---|
#5 | 2744517-5.patch | 4.2 KB | webflo |
#3 | 2744517-1.patch | 872 bytes | webflo |
Comments
Comment #2
webflo CreditAttribution: webflo at UEBERBIT GmbH commentedI had the same issue, i think we should convert Twig_Markup object to Drupal Markup object. Twig_Markup contains safe html already. Here is an example how it works in the menu.html.twig template.
Comment #3
webflo CreditAttribution: webflo at UEBERBIT GmbH commentedLinkGenerator::generate generates the link in this case. LinkGenerator itself should not depend on the rendering engine, we have to convert the safe twig object to a safe drupal object.
Comment #4
webflo CreditAttribution: webflo at UEBERBIT GmbH commentedComment #5
webflo CreditAttribution: webflo at UEBERBIT GmbH commentedComment #6
dawehnershould we call (string) $text maybe?
Comment #7
webflo CreditAttribution: webflo at UEBERBIT GmbH commentedI don't think so this would lead to double escaping see LinkGenerator::generate.
Comment #8
joelpittetThis is already accepted, this is just documenting it properly (see the rest of the description). Not sure if this needs a CR or not?
This seems like the right solution to me for now. The only better would be if we could replace
\Twig_Markup()
with ourMarkupInterface
, but that's not possible.Comment #9
alexpottThe fix does not match the issue summary and title. The solution looks sensible but I wonder if we need to convert the object? Maybe we can make the bits that chose whether to do auto-escape Twig_Markup aware?
Comment #10
dawehnerWell, conceptually I think mixing twig and render level objects is wrong. The render system should not know about how twig works internally. Converting from the domain of twig to the render system is what this patch is doing, right in the spot which still knows about twig, aka. the twig extensions system.
Comment #11
joelpittetFixed the issue summary and title. Though I like the intent and idea behind #9, I agree with @dawehner's rational in #10
Comment #12
alexpott@dawehner good point about twig and render.
Committed 1742237 and pushed to 8.3.x. Thanks!
Leaving as patch to be ported for cherry-pick to 8.2.x once 8.2.0 is released.
Comment #14
alexpottCommitted 5429c93 and pushed to 8.2.x. Thanks!