0

So I've been using token replace for a while now with great success in my themes. For instance I'm using it currently to show the real name of an author by using

<?php print token_replace('[node:author:url]', array('node' => $node)); ?>">

The issue is that I've recently added a byline field in my storys and not all stories have a byline. I've added it normally by using

<?php print token_replace('[node:field_byline]', array('node' => $node)); ?>

However when the field is empty it simply prints [node:field_byline] so I get stories like...

By: [node:field_byline]

Is there a way to check that the token field has something in it and only show that field if there's a value? I'd like to just show nothing if there are now authors for the byline. I tried using something like:

<? !empty [node:field_byline] ?>

but this hasnt worked. Ideas?

1

You can use the 'clear' option of token_replace to clear any tokens it can't find a replacement for.

<?php print token_replace('[node:author:url]', array('node' => $node), array('clear' => TRUE,); ?>">

As documented here: https://api.drupal.org/api/drupal/includes%21token.inc/function/token_replace/7.x

0

Maybe this could help?

<?php 
   $pos = strpos(token_replace('[node:field_byline]', array('node' => $node)), 'node:');

   if ($pos !== false) { // field is not empty
      print token_replace('[node:field_byline]', array('node' => $node));
   }
   else {
      print '';
   }
?>
  • That doesn't seem to do anything. It doesn't toss any errors either however. I just posts nothing at all no matter what. – doodirock Jan 3 '14 at 22:35
  • 1
    Figured it out! I just used the following and it worked fine. <?php if (token_replace('[node:field_byline]', array('node' => $node))): ?> <?php print token_replace('[node:field_byline]', array('node' => $node)); ?> <?php endif ?> If there is a better way to do it I'm all ears! – doodirock Jan 3 '14 at 23:00
0

Figured it out! I just used the following and it worked fine.

<?php if (token_replace('[node:field_byline]', array('node' => $node))): ?> <?php print token_replace('[node:field_byline]', array('node' => $node)); ?> <?php endif ?> 

If there is a better way to do it I'm all ears! – doodirock Jan 3 at 23:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.