How to use conditional not to print a field if empty ?

brakkar - August 17, 2006 - 16:43
Project:Content Templates (Contemplate)
Version:HEAD
Component:Miscellaneous
Category:support request
Priority:normal
Assigned:Unassigned
Status:closed
Description

Hi,
let's say I have a field called web_url :

<?php
foreach ((array)$field_web_url as $item) {
?>

<?php
print $item['view']
?>

<?php
}
?>

How should I code conditional so the field and its label will not be displayed if field is empty ?

Cordially,
Brakkar

#1

jjeff - August 17, 2006 - 16:53

Well this code doesn't output a field label, so you'll probably only get <p></p> as the output if there's nothing there.

But the thing to do is to look at the field's "value" attribute rather than the "view" attribute and see if it's empty. So your code would look something like this:

<?php
foreach ((array)$field_web_url as $item) {
  if (!empty(
trim($field['value'])) { // first we trim off any stray space characters, then check to see if anything is left
  
print $item['view'];
  }
}
?>

That is untested, but you should get the idea

#2

jjeff - August 17, 2006 - 16:53
Status:active» fixed

#3

Anonymous - August 31, 2006 - 17:00
Status:fixed» closed

#4

brakkar - October 3, 2006 - 13:26
Status:closed» active

Ok,
what I want to do is include the label of a field (simple text) in a conditional so if field is empty, label will not display neither.

I tried your code:

<?php
foreach ((array)$field_ticker_id as $item) {
  if (!empty(
trim($field['value'])) { // first we trim off any stray space characters, then check to see if anything is left
  
print $item['view'];
  }
}
?>

with a ticker_id field, but got this error message:

Parse error: syntax error, unexpected T_STRING, expecting T_VARIABLE or '$' in /big/dom/xmysite/www/drupal/sites/www.mysite.com/modules/contemplate/contemplate.module(455) : eval()'d code on line 3

Any clue why ?

Cordially,
Brakkar

#5

brakkar - October 3, 2006 - 14:29
Status:active» fixed

Ok,
found the solution.... if someone thinks the code is bad, please reopen the issue and explain why.
Meanwhile, here is what I use: it will print the field only if it has a value:

<?php
foreach ((array)$field_ticker_id as $item) {
if(!empty(
$item['view'])){
echo
"<h3 class=\"ticker\">Ticker Id</h3>"; print $item['view'];
}
}
?>

This is an example for a cck field called "ticker_id".

Brakkar

#6

jjeff - October 3, 2006 - 18:14

Looks good to me!

-j

#7

Anonymous - October 17, 2006 - 18:15
Status:fixed» closed

#8

NathanRAFT - March 24, 2007 - 17:28

I am still getting the h4 label showing even if there is no value in the field. Here is my contemplate code.

<?php
foreach ((array)$field_original_website as $item) {
if(!empty(
$item['view'])){
echo
"<h4>Original site</h4>";
print
$item['view'];
}
}
?>

Any ideas? I must be missing something simple here but can't figure it out.

Thanks!!

#9

gemini - June 5, 2007 - 00:35

This is how the regular field output looks like:

<div class="field field-type-text field-field-address">
  <h3 class="field-label">Address</h3>
  <div class="field-items">
    <?php foreach ((array)$field_address as $item) { ?>
      <div class="field-item"><?php print $item['view'] ?></div>
    <?php } ?>
  </div>
</div>

if we create condition mentioned above - that means that the whole layer will be output with the lable anyways. When I edit contemplates I have to add this condition on the outside of each layer like this:

<?php if(!empty($node->field_address[0]['value'])){?>
<div class="field field-type-text field-field-address">
  <h3 class="field-label">Address</h3>
  <div class="field-items">
    <?php foreach ((array)$field_address as $item) { ?>
      <div class="field-item"><?php print $item['view'] ?></div>
    <?php } ?>
  </div>
</div>
<?php } ?>

I use [0]['value'] only in the case when I output a single node since there are no other values in the same array (in my cases).
I think this type of condition needs to be pre-set in the contemplates, otherwise there is so much extra html code in the output in case of blank fields.

#10

gemini - June 5, 2007 - 01:08
Status:closed» active

Just realised that using [0]['value'] will work in any case - of single or multiple values in the array. There is one problem thought.. with fieldimage/imagecache ['value'] doesn't work - you have to use ['view'].

I think this is an issue where templates are incomplete because they don't have the condition not to output all that extra HTML code in case of blank fields.

Also, I wanted to note that the HTML structure in contemplates is not the same as default structure in regular CCK node output. CCK has "Display Fields" tab where you can set lables to show inline, above the fields or hide - default HTML code of contemplates is not the same and doesn't reflect "Display Fields" settings which I think it supposed to.

#11

gemini - June 5, 2007 - 01:32

Actually, your HTML structure is much better, but I don't like that it doesn't follow the "Display Fields" rules. May be there should be a CSS added.

#12

jrglasgow - May 13, 2008 - 02:23
Status:active» postponed (maintainer needs more info)

does this still need to be open?

#13

YesCT - November 6, 2008 - 18:04

This is very helpful!

#14

jrglasgow - November 7, 2008 - 00:24
Status:postponed (maintainer needs more info)» fixed

#15

System Message - November 21, 2008 - 00:32
Status:fixed» closed

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

#16

nirvanajyothi - May 15, 2009 - 19:52

Am adding the code that worked for me. Hope the template is alright. It also allows to add a background color in a table the dimensions of which can be changed.

<?php print $node->content['body']['#value'] ?><?php if(!empty($node->field_name[0]['value'])){?><table bgcolor="#99cccc" border="0.5" width="720"><tr>
<td><img alt="image" width="13" src="http://localhost/domain/sites/default/files/favicon.gif" /><?php print $node->field_name[0]['value'] ?></td>
</tr></table>
<?php } ?>

Thanks for all the hints.

#17

dsweeney0126 - September 24, 2009 - 09:36

Really helpful discussion. I'm using the code below to conditionally display the field label, but when the field has multiple entries, I get multiple labels. Any thoughts?

foreach ((array)$field_phases[0] as $item) {
if(!empty($item['view'])){
echo "Phases:";
}
}

#18

dsweeney0126 - September 26, 2009 - 21:59

Nevermind. Using the code in comment #9 solved the problem.

#19

wildchief - October 9, 2009 - 12:15

I had something similar where one of my fields I allowed users to add 10 fields. If they only filled in 5 of them I found that it still out put empty fields in my case as blank list items. This is how i solved it

Reasons Why

<?php
foreach ((array)$node->field_reasons as $item) {
     
$trimmed = trim($item['view']);
      if (!empty (
$trimmed)){
      echo
"<li>";
      print
$trimmed ;
      echo
"</li>";
      }
     }
   
?>

 
 

Drupal is a registered trademark of Dries Buytaert.