Needs review
Project:
Token
Version:
8.x-1.x-dev
Component:
Code
Priority:
Normal
Category:
Feature request
Assigned:
Unassigned
Reporter:
Created:
10 Feb 2020 at 13:21 UTC
Updated:
24 Feb 2026 at 17:57 UTC
Jump to comment: Most recent, Most recent file
Comments
Comment #2
jienckebd commentedComment #3
sanjayk commented@jienckebd I tried to check but it not reproduce. Please provide steps how to check the changes.
Comment #4
sanjayk commentedComment #5
modestmoes commentedI modified the previous patch to make sure that the definition of the url token is included in `field_token_info_alter` which will make the link token also appear in the 'show tokens' ajax box.
Comment #6
ijsbrandy commentedThe actual replacements of the url tokens are not created such as "absolute" or "relative". For example [paragraph:field_link:url:absolute] should create the absolute url.
Patch attached
Comment #7
berdirNeither patch is correct on its own, what's needed is a combined version. So that both link_field:url and link_field:url:absolute works. A test would be good as well. We have existing field tests, so just need to add a link field as well and then we can test the expected token output.
Comment #8
pobster commentedHow about utilising other stuff from elsewhere to make it more fully featured? It'll match other tokens then?
If this suits, I'm happy to write some tests - just we're ultra busy right now so I'd need to do it in a few weeks.
Comment #9
pobster commentedI guess now I think about it;
...would have been a bit cleaner. Meh, I'll alter that in a couple weeks when I can finally breathe after our big push.
Comment #10
berdirThe token system is recursive. If you define it as type url you shouldn't have to duplicate the code. You just need to handle the default token, and pass any nested tokens along like #6 is doing.
Comment #11
pobster commentedI'd misunderstood what you said in #7 then.
Simple fix.
Problem is, as you requested;
But ... link_field:url doesn't work like this, and IDK why that is yet (I've no time to dig into it).
Comment #12
berdirYes, that's what I mean with combine the two patches. See how it works for the daterange type for example. If it's exactly "url" or "0:url" then you need to implement replace it yourself, if it's a nested token, you pass it along.
Comment #13
ijsbrandy commentedIndeed stupid of me not seeing that.
Tests added to patch.
Furthermore, the link field gives the possibility to also add an external link. For example https://www.drupal.org/foo/bar. When url:path token is added you expect to print the path of that external url "/foo/bar". This is currently not implemented and will return "/".
By validating if the url is external we can specify the correct results on every external url:* token. Although the external url implementation works, I am not sure if there are any core functionality that could replace these implementations.
Comment #15
berdirLooks like that's triggering an alias lookup in the existing tests. I've yet to review what the code there is doing, but I'm not sure it should be combined into this issue. That other change will affect all existing link tokens and it's even more things to write tests for.
Comment #16
pobster commentedIf you can clarify ... this is what's required, come end of next week - I'll have some time to write some tests.
Comment #17
ijsbrandy commented@pobster:
Good one to check the actual properties. A few thinks that should be discussed about.
First, should we not only generate the tokens that core does not provide as what this module initially is for? The tokens uri and title are already provided by core. Probably by just checking if the property_name equals to url we only generate the tokens that core does not provide, right? The following code should then be enough:
As already used in patch 13 the Link FieldType has a method getUrl() that also includes the url options. see web/core/modules/link/src/Plugin/Field/FieldType/LinkItem.php.
@Berdir:
Comment #18
berdirDrupal core does not provide any field tokens. They exist right now because token.module generically provides tokens for defined properties, but with this snippet, we are probably skipping that generic implementation and need to check for it. A test will help to verify that.
Comment #19
ijsbrandy commentedComment #21
pobster commentedThis is correct.
Comment #22
lucasantunes commentedI'm attaching a mix of #16 and #19.
I think we still need the general implementation, because the same problem reported here also happens with the Link field. I have an external link with query strings and it gets HTML encoded, rendering
&instead of&, for instance.Therefore, I used the same technique of applying a
Markup::create()to the strings so that they're not encoded afterwards.I don't know if it's the best approach, but it is what works for me :)
I've also updated the Tests with the
pathproperty and a query string in one of the fields. I wasn't able to actually test it, though. I'm having aBase table or view not found: 1146 Table 'default.test24098324path_alias' doesn't exist in /var/www/docroot/core/lib/Drupal/Core/Database/Statement.php:59exception when trying to run it... It's my first patch, as well as my first contact with Tests, so I don't know if I did something wrong.In a nutshell:
Markup::create()to the first two if/else statements in the newlinkimplementation.Markup::create()to thepath,absolute,relativeandbriefcases in the URL tokens implementation.multivalued_field_linkfield in the tests and its respective expected tokens.pathproperty to the expected tokens as well.Anyways, I hope it can help someone!
Comment #23
lucasantunes commentedSorry, wrong filename, I didn't notice the test report had been considered as a comment 😅
Comment #27
duaelfrPushed and rerolled patch from #22 in a MR
Comment #28
gaia commentedHi there,
i need a token for the URL of a link field and found this here.
Unfortunaletely these patches cannot be applied to the latest token release.
Any advice or help is appreciated.
Thanks
Comment #29
duaelfrThe patch in my Merge Request applies on 1.x and 1.11.
If you don't know how to apply patches from a Merge Request, you can read this answer on stackoverflow.
In that specific case you can use that URL: https://git.drupalcode.org/project/token/-/merge_requests/24.patch
Comment #30
gaia commentedThank you very much for the hint, DuaelFr.
Comment #31
klidifia commentedIf the token resolves to an internal URL (e.g. an internal node selected from an autocomplete lookup), this won't render as a link.
Have taken the 24.patch on GitLab and added:
Before the:
Comment #33
klidifia commentedUnsure how to introduce path_alias into the test setup for this as TokenFieldUiTest extends TokenTestBase
Comment #34
burnellw_cit commentedComment #35
WebbehWhat does the patch in #34 do? How is it different than #31? Can you provide any context to what the patch fixes?
Comment #36
bcobin commentedI'm trying to write an embedded .mp3 player for a file field (displayed as a link) and Token appends "internal:" to the path, causing the player to fail. It's a site with not a ton of audio, so I'm working around it by rewriting the title to point to the file URL and using [node:field_audio_file:title] to send the URL to the player, but there should be an option to use the path without appending "internal:" to it.
My issue seems to be related to the discussion here, so I'm passing along the use case and trying to stay in the loop. This is a Drupal 10 site and the patch looks fairly substantial, so I'll hope for a commit. Thanks!
Comment #37
oskar_calvo commentedI'd like to know if it's posible to apply the patch to drupal 9.x and 10.x
Thanks.
Comment #38
sharique commentedI've re-rolled the patch against latest release 1.15.
Comment #39
duaelfrRerolled MR on latest dev version.
Comment #40
socialnicheguru commentedIs this issue similar or overlapping with this one, #1198032: Add modifiers to the [current-page:url] token to include the pager and the query string ?
Comment #41
pobster commentedNot especially ^^ this is distinct.
Comment #42
pfrenssenNeeds to be rerolled now that #3545229: Convert hooks to OOP is in.
Comment #43
pfrenssenI did an iteration on it, but there is still an issue with this. It looks like there is a difference between
[node:field_link_2:path]and[node:field_link_2:url:path]:[node:field_link_2:path]seems to return the relative path including the base URL (/web/foo/bar).[node:field_link_2:url:path]seems to return the relative path without the base URL (/foo/bar).Comment #45
liam morlandI have rebased the merge request and adjusted the tests so that they now pass. I am not sure that the test changes are correct; it may be that the test was right and the code is wrong.
Comment #46
liam morlandThis patch is for 8.x-1.16.
Comment #47
sjpeters79 commentedApplies to latest 1.17 release.
Comment #48
sjpeters79 commentedSyntax error in last patch...
Comment #49
g4mbiniVery useful issue. It solves a use case I had on a client project. Thanks for the patch!