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

foreach ((array)$field_web_url as $item) {
print $item['view']
}

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

Cordially,
Brakkar

Comments

jjeff’s picture

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:

 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

jjeff’s picture

Status: Active » Fixed
Anonymous’s picture

Status: Fixed » Closed (fixed)
Robardi56’s picture

Status: Closed (fixed) » 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:

 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

Robardi56’s picture

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:

 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

jjeff’s picture

Looks good to me!

-j

Anonymous’s picture

Status: Fixed » Closed (fixed)
nathanraft’s picture

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

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!!

gemini’s picture

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.

gemini’s picture

Status: Closed (fixed) » 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.

gemini’s picture

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.

jrglasgow’s picture

Status: Active » Postponed (maintainer needs more info)

does this still need to be open?

YesCT’s picture

This is very helpful!

jrglasgow’s picture

Status: Postponed (maintainer needs more info) » Fixed

Status: Fixed » Closed (fixed)

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

nirvanajyothi’s picture

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.

dsweeney0126’s picture

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:";
}
}

dsweeney0126’s picture

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

wildchief’s picture

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

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

#16 worked great for me, thanks!

Rosamunda’s picture

I´ve tried #16, but my matrix field won´t appear.
I´m printing it like this:

<?php if(!empty($node->field_curso_cronograma[0]['value'])){?>
<?php print $node->field_curso_cronograma[0]['value'] ?>
<?php } ?>

Any ideas? Maybe it doesn´t work because it is a matrix field and it should be printed a different way?

Thanks for your help!!!
Rosamunda

Prodigy’s picture

@ Rosamunda

Try adjusting line 2 to read the following:

<?php print $node->field_curso_cronograma[0]['view'] ?>
youssefr’s picture

Hi everyone,
I tried #21 and #22 and they are both working great for me and I am getting the result I am looking for. print $node->field_curso_snippets[0]['value'] or print $node->field_curso_snippets[0]['view'] did not change anything for me. The purpose here is to show and style only the added values for my multi-value CCK field "field_snippets". In my case, I have 4 values for this CCK field (I am letting the users publish a maximum of 4 snippets/values).
Question: how do you hide the style of background, border...from displaying when the user has not added any values?

Thank you.

evinweissenberg’s picture

The solution is very simple and it goes like so...

<table>


                    echo "<tr>";
                    foreach ($obj->getArray() as $key => $value)
                    {
                        if($value == NULL)
                        {

                            //skip

                        }
                        else
                        {

                        echo "<td>$value</td>";

                        }

                    }
                    echo "</tr>";                   
 

</table>