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
There is a module that would
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.
I know what the value is, I
I know what the value is and the field name, I just need to know how to construct the conditional statement in Drupal 7.
The Solution
<?php if($content['field_example']['#items']['0']['value']==1):?>Value1<?php elseif($content['field_type']['#items']['0']['value']==2):?>Value2<?php endif?>More concise version:
<?php $value=($content['field_example']['#items']['0']['value']);if($value==1:?>Value1<?php elseif($value==2):?>Value2<?php endif?>Yup
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:
I get two "Notice: Undefined index: field_open_enrollment in include()" errors that point to the two conditional statements above.
Any thoughts?
It's probably due to ['1'],
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.
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.
..
Another maddening problem is detecting whether or not a node reference field has a value or not. I can't pin it.
Any thoughts?
I have the same problem, I
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.
Obviously I'm doing something wrong.
Any help would be much appreciated.
Try this:
@mory
<?php $value=($content['field_open_enrollment']['#items']['0']['value']);if($value==1:?>Closed<?php endif?>Parse Error
Thanks undoIT,
I did what you suggested.
but I get this error: Parse error: syntax error, unexpected ':' in /home/sites/all/themes/tesas/node--tesas.tpl.php on line 51
Got it
Ok,
after much hair pulling I finally got my code working,
I just needed the "['#items']" and ['value'] part.
Thanks for everything !!!
I had this use case where I
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 -
But I had to render the content such that images would be in between content of other fields, some thing like below -
This thread, and your particular reply @mory did the trick for me and saved me a lot of fighting my way around. Many Thanks :-)
I have the same error as
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.
Subscribe
Having the same problem as others. Can't test for a conditional value.
Working Now
It's working for me now.
if ($content['field_resource_proxy']['#items']['0']['value']=='Yes') {
print "it works";
}else{
print "it doesn't work";
}
Not working for image field
The above said procedures are not working for me.
Kindly help me to display the another image when no image is uploaded
Two things
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.
same problem
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.
About between clause
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
Please help me,
i Had tried to prepare with view cal but not succeeded ...