This is a follow up from [2646316-15].

While writing tests, we discovered that field tokens are not replaced by empty strings when the field does not have a value. The this script and its output for a visual example: https://gist.github.com/juampynr/e949f2c20e4ea92b26b1.

So far I can see the following options:

a) Leave the token there.
b) Replace the token by an empty string.

I can see no benefit on leaving the token there. What do the others think?

Issue fork token-2648180

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

juampynr created an issue. See original summary.

hussainweb’s picture

I would say that it should be replaced by an empty string. That is what I would expect if I am using this token.

juampynr’s picture

Status: Active » Needs review
StatusFileSize
new1.92 KB

Here is a patch where tokens for empty fields are replaced by an empty string.

juampynr’s picture

StatusFileSize
new1.37 KB

Here is an even simpler approach where we let the renderer to render the empty field.

berdir’s picture

Status: Needs review » Reviewed & tested by the community

Makes 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.

berdir’s picture

Assigned: Unassigned » dave reid
juampynr’s picture

StatusFileSize
new1.16 KB

Re-rolling.

juampynr’s picture

Bumping this up.

NetNerdy’s picture

Status: Reviewed & tested by the community » Needs work

patch #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...

pminf’s picture

StatusFileSize
new1.15 KB

Since Issue#2621598 patch#7 does not apply anymore. Let's reroll (co-work with boogiewoogieman).

pminf’s picture

I've tried to add a test case for empty field value but failed. Could anyone help me, please? I'm fairly new to this.

berdir’s picture

You 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.

jasonawant’s picture

Assigned: dave reid » Unassigned
Status: Needs work » Needs review
StatusFileSize
new2 KB
new2.54 KB
new907 bytes

Here'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.

Status: Needs review » Needs work

The last submitted patch, 13: replace_empty_field_tokens-test-only.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

jasonawant’s picture

Status: Needs work » Needs review

Changing back to needs review

jasonawant’s picture

After 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/...

* - clear: A boolean flag indicating that tokens should be removed from the
* final text if no replacement value can be generated.

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.

jasonawant’s picture

I'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

jasonawant’s picture

Status: Needs work » Needs review
StatusFileSize
new5.18 KB
new5.5 KB

I 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.

Status: Needs review » Needs work

The last submitted patch, 18: replace_empty_field_tokens-2648180-17.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

saidatom made their first commit to this issue’s fork.

saidatom’s picture

Status: Needs work » Needs review
berdir’s picture

Status: Needs review » Needs work

alemadlei made their first commit to this issue’s fork.

alemadlei’s picture

Hello, 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.

alemadlei’s picture

Status: Needs work » Needs review
joseph.olstad’s picture

StatusFileSize
new4.69 KB

verbatim as MR 66

saidatom’s picture

Status: Needs review » Needs work
saidatom’s picture

Status: Needs work » Needs review
redbrickone’s picture

StatusFileSize
new788 bytes

I had to reroll the latest patch in order to get it to apply cleanly on Drupal 11.3.8.