How can I hide a DIV if ther's no value in that Content in a node--customtpl.tpl.php file? I have used a code something like this, but if hides everything weather it has data or not. Can anyone help me with this code?

I want to hide this "special-offer" div if there's no value in that field in drupal (this_is_a_drupal_field).

if ($content['this_is_a_drupal_field']) 
  print "<div class='special-offer'>";
  print render($content['this_is_a_drupal_field']); 
  print "</div>";

<?php if(empty($content['this_is_a_drupal_field'])){ ?>

Your if statement needs a condition, ex: 'greater than' > 'less than' < 'equal to' == or 'not equal to' !=

The special NULL value represents a variable with no value.

So try:

<?php if ($content['this_is_a_drupal_field'] != NULL): ?>
  <div class='special-offer'>
    <?php print render($content['this_is_a_drupal_field']); ?>
<?php endif; ?>

So basically we're saying if $content['this_is_a_drupal_field'] is not equal to empty print...

  • Nope it Prints the <div class='special-offer'> without the content as well. :( – UI-UX Feb 27 '16 at 5:41
  • @UI-UX impossible, did you clear/flush the cache? – No Sssweat Feb 27 '16 at 6:13
  • Yes this was correct and fixed my problem. It was not with the cash. this came from a field collection, and automatically passed the add edit links. I removed those and problem was solved. Thanks No Sssweat. AWESOME. – UI-UX Feb 27 '16 at 15:13
  • 1
    @NoSssweat You don't necessarily have to have a condition in an if statement. if ($content['this_is_a_drupal_field']) is a completely valid statement and is equals to FALSE if the given value has a falsy value, for example an empty string or array. – Елин Й. Feb 27 '16 at 18:50
  • @ЕлинЙ. interesting, thx. – No Sssweat Feb 27 '16 at 22:09

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.