Hello,

I have a token in an invoice template. It well works but I wish to display a legend only if it is not empty.
How to test it in PHP?

I've tested, the code below, but I have a parse error

if (!empty("[order-vat-number]")) {
 ...

Thanks in advance

Comments

Dave Reid’s picture

Status: Active » Fixed

I'm assuming this is a node token.

$nid = 1; // Replace with proper NID value here.
$node = node_load($nid);
$tokens = token_get_values('node', $node);
$tokens = array_combine($tokens->tokens, $tokens->values);
return !empty($tokens['order-vat-number']);

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

craigritchie’s picture

I'd like to test if user tokens are empty -- (first name, last name). Any help? Thanks!

Bairnsfather’s picture

Sorry 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 empty changes behaviors based on the version of PHP caused me to re-think my strategy.

So I went with strlen instead (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

misterpo’s picture

Hello 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 :

if (!empty($entity->field_bien_louer_lien_reka)) { print "<div class=\"reka\">Réservation Reka :<a class=\"btn btn-theme\" href=\"[node:field_bien_louer_lien_reka]\" target=\"_new\">Réservez</a></div>"; }

Any idea of how to achieve this ?

Bairnsfather’s picture

Take what I say with a grain of salt. As I recall empty was 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.

misterpo’s picture

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

Bairnsfather’s picture

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

misterpo’s picture

Version: 6.x-1.12 » 7.x-1.5

My 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 ?

misterpo’s picture

Hi,

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 :

$reka= '[node:field_bien_louer_lien_reka]';
if (strlen($reka) <> 33) { print "<div class=\"reka\">Réservation Reka :<a class=\"btn btn-theme\" href=\"[node:field_bien_louer_lien_reka]\" target=\"_new\">Réservez</a></div>"; }

Ugly but works...

p0832414’s picture

Sorry for re-opening a closed thread, but I wanted to share the solution I came up with. I think it is self-explanatory.

<?php
...
$token = token_replace('[node:example-field]', array('node' => $node), array('clear' => 'clear'));
if (strlen($token) > 0) {
  print "The token is not empty";
} else {
  print "The token is empty";
}
...
?>
MrPeanut’s picture

Issue summary: View changes

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

tkuldeep17’s picture

Version: 7.x-1.5 » 7.x-1.x-dev
Status: Closed (fixed) » Needs review

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

/**
 * Check if string is token.
 * @return boolean True if string is token else FALSE.
 */
function check_string_token($string) {
  preg_match_all('/
    \[             # [ - pattern start
    ([^\s\[\]:]*)  # match $type not containing whitespace : [ or ]
    :              # : - separator
    ([^\[\]]*)     # match $name not containing [ or ]
    \]             # ] - pattern end
    /x', $string, $matches);
  if ($matches[0] == '' || $matches[0] == NULL) {
    return FALSE;
  }
  else {
    return TRUE;
  }
}

Chris Matthews’s picture

Assigned: selinav » Unassigned

Unassigned @selinav

jasonawant’s picture

Status: Needs review » Closed (outdated)
Related issues: +#2648180: Replace field tokens by empty strings when there is no field value

Closing 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