Closed (outdated)
Project:
Token
Version:
7.x-1.x-dev
Component:
Documentation
Priority:
Normal
Category:
Support request
Assigned:
Unassigned
Reporter:
Created:
24 May 2010 at 12:41 UTC
Updated:
9 Jan 2023 at 17:17 UTC
Jump to comment: Most recent
Comments
Comment #1
dave reidI'm assuming this is a node token.
Comment #3
craigritchie commentedI'd like to test if user tokens are empty -- (first name, last name). Any help? Thanks!
Comment #4
Bairnsfather commentedSorry to dredge this back up, but in looking for this same answer I took came across this thread. I'm guessing you, Craig, already figured something out, but I figured I would post what I could to help others.
I wrote code just like selinav and it didn't work.
I had a node field of text > text field. After fixing the code, the test worked if there was only one word but if there were two words in that field it failed. That, along with the fact that
emptychanges behaviors based on the version of PHP caused me to re-think my strategy.So I went with
strleninstead (my host is just a hair past php 5.3.0 so I hope I'm safe from future changes).Basically all I wanted to do was use http://drupal.org/project/auto_entitylabel to create automatic node titles based on an organization's name. But if there was a short version of the name I wanted it in parentheses after the long version of the name. Since it didn't make sense to require both a long and short version of a name, I knew I didn't want empty () after names with no short version.
This is what I came up with:
if (strlen('[node:field-organization-name-short]') > 0) { echo "([node:field-organization-name-short])"; }Drupal 7.22, Token 7.x-1.5, and Automatic Entity Label 7.x-1.1
Comment #5
misterpo commentedHello all,
I have the same need to conditionally check a token's value. I would like to display a node token only if it is not empty.
I have tried Bairnsfather's solution without success - strlen('[node:field-organization-name-short]') returns the length of the "[node:field-organization-name-short]" chain -.
So I have given a try to the $entity object, without success. My code is :
Any idea of how to achieve this ?
Comment #6
Bairnsfather commentedTake what I say with a grain of salt. As I recall
emptywas less than desirable because (little hazy here) it seems there are some values that do not equate to empty although for most intents and purposes they are. However, their string length is 0. Thus I went with strlen.If you're like me you have both Token and Entity Token installed. Not sure the state of your code now, but if you are sure the logic is correct just swap out tokens.
You print a token, but looking at the code again, you do not enclose your token in brackets in if(). Also, I would consider reinserting the token. The syntax looks wrong, but I've tried to keep from delving into php, so thus the grain of salt thing. Basically I got started here at Drupal.org, then had to read a bunch at php.net to figure out why empty was not working the way I expected. Sorry I don't have a definitive, few word answer.
Comment #7
misterpo commentedI have tried to access the filed value through the $entity object, not the token itself.
I don't understand how your solution using the strlen() functions works, it always gives the same value on my side, that is 33.
Tokens seem to work in PHP only with echo or print instructions...
Comment #8
Bairnsfather commentedYou probably know more about php than I do. :-) Perhaps I ended up using tokens because I tried direct access through php first. :-)
My code is within the context of https://drupal.org/project/auto_entitylabel which creates a tab in the content type admin area. I want to rewrite the node title but not fill it with needless characters.
Perhaps your code is outside of where a token filter sees it?
Comment #9
misterpo commentedMy code in the Body field formatted with PHP code.
I am still stuck with this problem.
Does somebody see any other way to perform this null test ?
Comment #10
misterpo commentedHi,
I have found an ugly workaround to this problem.
Drupal seems to evaluate the node token only if it is not empty. So if the field is empty, the string length of the node token will be different from the string length of the token character chain.
- String length of [node:field_bien_louer_lien_reka] if the field is empty : 33
- String length of [node:field_bien_louer_lien_reka] if the field is not empty : different from 33 but not equal to the field value length !
My code is :
Ugly but works...
Comment #11
p0832414 commentedSorry for re-opening a closed thread, but I wanted to share the solution I came up with. I think it is self-explanatory.
Comment #12
mrpeanut commentedI attempted the same code as @misterpo. However, my token is always returning a string length of 48, regardless of whether there's actually data in the token. The token is
[commerce-order:commerce_coupon_order_reference]. It should be just returning blank or the coupon code if I understand correctly.Comment #13
tkuldeep17 commentedSorry for reopening it..
For checking if string is token or not we can take use of function token_scan and can create a new function in token.inc file.
Comment #14
chris matthews commentedUnassigned @selinav
Comment #15
jasonawantClosing as outdated. Feel free to reopen this issue if you like.
Added related issue #2648180: Replace field tokens by empty strings when there is no field value