Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
By wheretoplaygames on
I'm trying to create a menu that opens external links in a new tab but leaves internal ones alone.
I know i can use .setAttribute to add the target, but how do i determine if the link is external? Seems like i should be uering URL::$external, but not sure how.
It would be infinitely helpful if the API pages showed examples of use.
Comments
Try UrlHelper::isExternal
Try UrlHelper::isExternal
Documentation: https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Component%21Util...
Any examples? My syntax throws errors
{% if UrlHelper::isExternal(item.url) %}
Not sure why that doesn't work. Throws: Unexpected token "punctuation" of value ":"
UrlHelper::isExternal() is for PHP
I didn't realize you were asking about usage in a Twig template.
UrlHelper::isExternal()
is for use in your PHP code. Drupal's API pertains to the PHP portion of your themes & modules. For Twig documentation, see https://twig.sensiolabs.org/doc/2.x/.To achieve specifically what you had in mind, I think the method used in the answer to this question on Stack Exchange should help: https://craftcms.stackexchange.com/questions/2544/whats-the-best-way-to-...
Hope it helps!
For now ...
see https://twig.sensiolabs.org/doc/1.x/
Wait for D8.4 for https://twig.sensiolabs.org/doc/2.x/
For now.... what?
Thanks for the link to twig, but not sure what i'm looking for there...
Use url.external
A few examples: