Needs review
Project:
Token
Version:
8.x-1.x-dev
Component:
Code
Priority:
Normal
Category:
Task
Assigned:
Unassigned
Reporter:
Created:
11 Jan 2016 at 13:33 UTC
Updated:
5 May 2026 at 13:58 UTC
Jump to comment: Most recent, Most recent file
Comments
Comment #2
hussainwebI would say that it should be replaced by an empty string. That is what I would expect if I am using this token.
Comment #3
juampynr commentedHere is a patch where tokens for empty fields are replaced by an empty string.
Comment #4
juampynr commentedHere is an even simpler approach where we let the renderer to render the empty field.
Comment #5
berdirMakes sense to me too. I'll give Dave a chance to give his OK on this one if has a minute as it's related to #1614456: [current-page:query:?] should fall back to empty string, which I'm not sure if we should do.
Comment #6
berdirComment #7
juampynr commentedRe-rolling.
Comment #8
juampynr commentedBumping this up.
Comment #9
NetNerdy commentedpatch #7 is not running for me: Drupal 8.1.1 and Token 8.x-1.x-dev and PHP 7.x
some patches using (e.g. in token.tokens.inc around line 1265)
($entity->get($field_name)
(e.g. "Chained tokens for field tokens")
and some others
($entity->get($name)
(e.g. this one and "Allow field tokens also for base fields")
i'am not sure what's right or wrong...
Comment #10
pminfSince Issue#2621598 patch#7 does not apply anymore. Let's reroll (co-work with boogiewoogieman).
Comment #11
pminfI've tried to add a test case for empty field value but failed. Could anyone help me, please? I'm fairly new to this.
Comment #12
berdirYou defined a non-existing property, that's not the same as an empty field at all.
Testing an empty field requires that you create an entity which has no value for the field and pass that in as data and test for a valid token.
Comment #13
jasonawantHere's a patch that includes the code change and updates tests. Feedback welcome!
This patched test only validates the text field type as an empty string. Should we consider validating other field types?
I'm surprised this issue hasn't gained more attention. I'll speculate that most field token usage are for required field, and as a result, this issue isn't common.
I do wonder if this change gets committed; how it would impact users. I imagine there could be custom code out there that is stripping un-replaced tokens. I'm guessing the impact would vary by implementation. For example, unsetting an array key or removing an object property if detecting an un-replaced token would no longer work.
Comment #15
jasonawantChanging back to needs review
Comment #16
jasonawantAfter some discussion with teammates, I was pointed to Drupal core Token::replace() method's options parameter includes a boolean option, clear.
See https://git.drupalcode.org/project/drupal/-/blob/10.1.x/core/lib/Drupal/...
It looks like the Token module is using this approach in this UrlTest, https://git.drupalcode.org/project/token/-/blob/8.x-1.x/tests/src/Kernel..., but I don't think any where else.
Comment #17
jasonawantI've come across a scenario where the token for an image media's file token is not being replaced when the optional (not required) media entity reference field does not have a value.
Token: [node:field_media:entity:field_image:entity:url]
I've also come across a few other related issues
Comment #18
jasonawantI tried to create a tests for the media entity file token replacement to demonstrate what I think would expected behavior, replacing the a token with empty string if it can not be evaluated/replaced.
Comment #22
saidatomComment #23
berdirComment #25
alemadleiHello, from the previous MR it sometimes worked for me. However, when having URL fields it would fail. So instead of just letting it continue, I'm shorting it so that it replaces and then continues to next token.
I didn't do anything with the test, but maybe this will help other people. On my end it work with the general tests that the previous one was passing and fixed the ones it was failing.
Comment #26
alemadleiComment #27
joseph.olstadverbatim as MR 66
Comment #28
saidatomComment #29
saidatomComment #30
redbrickone commentedI had to reroll the latest patch in order to get it to apply cleanly on Drupal 11.3.8.