Closed (fixed)
Project:
Twig Tweak
Version:
3.x-dev
Component:
Code
Priority:
Normal
Category:
Bug report
Assigned:
Unassigned
Reporter:
Created:
12 Jul 2021 at 03:34 UTC
Updated:
22 May 2024 at 05:56 UTC
Jump to comment: Most recent, Most recent file
Comments
Comment #2
cherrol commentedComment #3
cherrol commentedComment #4
tammycao commentedComment #5
smustgrave commentedTook a different approach to it.
The patch in #4 still rendered external links as internal. Example https://ddev-drupal/https://www.google.com
Not sure if this is the intended use of this function. Says on https://www.drupal.org/docs/contributed-modules/twig-tweak-2x/cheat-shee... it should be internal
but will let the maintainer decide that.
Comment #6
chi commentedWhat's the use case for this? Why do you pass ready to print URL to
drupal_url()?Comment #7
chi commentedComment #8
silverham commentedHi @Chi,
In built Drupal url/link in twig has limitations. such as
['#url'].setOption('fragment', html_id)due to twig sandbox policy. So instead I can use twig tweak to recreate the link of the url in Entity's link field. However if the link is external the code crashes :-(e.g.
Assuming a link with text "products" and I want html with custom icon / text prefix.
<a><span class="prefix">For more information, go to </span><span class="text">products</span></span class="icon"><span></span></a>So field twig could be (if external only):
Comment #9
silverham commentedComment #10
chi commented@silverham, I think such cases should not be handled in Twig.
Though you could just append the anchor to the generated URL.
Comment #11
silverham commentedHi @Chi That is very true. Good point!
But we can not be sure that the user will input a hashtag there already so it must be removed initially, if it exists, at least haha.
<a href="{{ item.content['#url']|render|preg_replace('/#.*$/', '') ~ '#my-anchor' }}">[...]</a>Comment #12
prashant.cThe solution provided to check the external path
if (UrlHelper::isExternal($user_input)) {in the patch provided by #5 seems fine but shouldn't this condition come after theApart from this, the function doc block should also be modified because currently it says
Comment #13
pradhumanjain2311 commentedI made changes as per comment #12.
Please review.
Comment #14
simon georges commentedThe previous patch cannot work, considering it will add a "/" on external urls before testing them for externality, so the patch in #5 is the correct one, and still applies cleanly on current version.
Comment #15
simon georges commented(in our case, we wanted to add a custom class on the link to be able to style it specifically)
Comment #17
prashant.cCreated MR from the patch provided by #5. Could not find any issues while testing locally.
Thanks!
Comment #19
chi commentedThank you.
Comment #21
cherrol commented