I am trying to customize the node.tpl template for a Drupal 7 site so that there is a conditional statement based on the value that has been selected for a particular field linked to the node. What I want to achieve is something like this:

If field_example = "Value1", echo "The first value", elseif field_example = "Value2", echo "The second value", else echo "No value selected".

In Drupal 6 I was able to do this as follows:
<?php if(content_format('field_example',$field_example[0])=='Value1'):?>The first value<?php elseif(content_format('field_example',$field_example[0])=='Value2'):?>The second value<?php else:?>No value selected<?php endif?>

I cannot figure out the equivalent in Drupal 7. Any suggestions?

Comments

nevets’s picture

There is a module that would help with that, since I can not get to the download section, the search I use is on "fields", "7.x" and the string "field value". There is a module that makes it easy to get the value given $node and the field name.

Coupon Code Swap’s picture

I know what the value is and the field name, I just need to know how to construct the conditional statement in Drupal 7.

Coupon Code Swap’s picture

<?php if($content['field_example']['#items']['0']['value']==1):?>Value1<?php elseif($content['field_type']['#items']['0']['value']==2):?>Value2<?php endif?>

Coupon Code Swap’s picture

<?php $value=($content['field_example']['#items']['0']['value']);if($value==1:?>Value1<?php elseif($value==2):?>Value2<?php endif?>

seaneffel’s picture

I've got a similar problem and I can't figure out why your example doesn't work for me - it looks like it should.

I've got a text field using an allowed values list of either "Open" or "Closed". So my example looks like this:


<?php if($content['field_open_enrollment']['#items']['1']['value']=="Closed"):?>
	Closed
<?php elseif($content['field_open_enrollment']['#items']['1']['value']=="Open"):?>
	Open
<?php endif?>

I get two "Notice: Undefined index: field_open_enrollment in include()" errors that point to the two conditional statements above.

Any thoughts?

nevets’s picture

It's probably due to ['1'], if the field only allows for one value that should be [0], note in this case you should be able to render/print the value directly since the value you are checking for is the one you are printing

seaneffel’s picture

It was enough to drive a person crazy.

I knew that my little snippet was pretty tight, but what was working against me was that the field was "hidden" in the content type. To get this to work for me, I had to make the field visible in the content type and then add a "hide($content['field_open_enrollment']);" to the node.tpl.php so I could later display it conditionally. That was about two hours of looking until I found something by WorldFallz. I'll go get that node and link it here if I can find it again.

seaneffel’s picture

Another maddening problem is detecting whether or not a node reference field has a value or not. I can't pin it.


<?php if($content['field_d7_nodereference']==NULL):?>
		Print something if it's empty.
<?php else: ?>	
		Print something if it's full.
<?php endif?>

Any thoughts?

morybel’s picture

I have the same problem, I can't figure this out. I've been searching the web, but nothing applies to my situation. I'm pulling my hair for days now... :-S

I have 3 fields:
field_spouse_yesno (List integer with 1|yes and 2|No)
field_spouse_name (text)
field_ex_name (text)

All I want to do is to print spouse_name if field_spouse_yesno = Yes, otherwise print field_ex_name

It's been working on D6 and I am currently transferring my site to D7 and I can't get this to work at all.
I can print my fields separately, so I know I have data.

<?php  if ($content['field_spouse_yesno'][0] == '1' ) {  
print render($content['field_spouse_name'][0]);
} else { print render ($content['field_ex_name'][0]) ; 
} ?>

Obviously I'm doing something wrong.
Any help would be much appreciated.

Coupon Code Swap’s picture

@mory

<?php $value=($content['field_open_enrollment']['#items']['0']['value']);if($value==1:?>Closed<?php endif?>

morybel’s picture

Thanks undoIT,
I did what you suggested.

<?php $value=($content['field_spouse_yesno']['#items'][0]);if($value==1:?>closed
<?php endif?>

but I get this error: Parse error: syntax error, unexpected ':' in /home/sites/all/themes/tesas/node--tesas.tpl.php on line 51

morybel’s picture

Ok,
after much hair pulling I finally got my code working,

 <?php
   if ($content['field_spouse_yesno']['#items']['0']['value']=='1') {
      print render($content['field_spouse_name'][0]);
    
} else {
    print render($content['field_ex_name'][0]);}
?>

I just needed the "['#items']" and ['value'] part.

Thanks for everything !!!

sumeet.pareek’s picture

I had this use case where I had an image field which allowed for unlimited values, and then I had some other fields too. The data of the image fields (all the images, say 3) would show together, something like below -

* one (text)
- value
* two (text)
- value
* three (images multiple)
- img 1
- img 2
- img 3
* four (text)
- value

But I had to render the content such that images would be in between content of other fields, some thing like below -

* one (text)
- value
* three (images multiple)
- img 1
* two (text)
- value
* three (images multiple)
- img 2
* four (text)
- value
* three (images multiple)
- img 3

This thread, and your particular reply @mory did the trick for me and saved me a lot of fighting my way around. Many Thanks :-)

vaccinemedia’s picture

I have the same error as above (Undefined index: field_link) where I have a field called "link" or machine name "field_link" but if the value is zero / null I get the error Undefined index. How can I test for if the value is there or not?

I'm a Drupal Website Developer and Google Street View Trusted Photographer.

stinky’s picture

Having the same problem as others. Can't test for a conditional value.

stinky’s picture

It's working for me now.

if ($content['field_resource_proxy']['#items']['0']['value']=='Yes') {
print "it works";
}else{
print "it doesn't work";
}

vmevada102’s picture

The above said procedures are not working for me.

Kindly help me to display the another image when no image is uploaded

nevets’s picture

Two things

a) Given the age of this thread I would suggest opening a new one.

b) It helps if you let people know what you have tried, simply stating something is not working does not tell people enough.

bmar777’s picture

The field category below only has 3 values, how can I print different class base on value of 'field_category?

if ($field_cat = render($content['field_category'])) print ''.$field_cat.'';

I have tried the advised methods but nothing seems to work. Any Suggestions? Thank you for your help.

vmevada102’s picture

i have created the one content type with the name "result_analysis"

this is having the two fields in it,
1. nod title
2. field_per_result : this display the percentages of the students

Now i would like to prepare the table with the details like

Result analysis Disitnction(>70%) First Class (BETWEEN 60-70) Secondclass (Below 60)
Total Number of Student 10% 15% 75%

Please help me,

i Had tried to prepare with view cal but not succeeded ...