In my template, I have set it up so it only prints the quote field if the field has text:
<?php $quote = $node->field_quote[0]['value'] ?>
<? if (isset($quote)) : ?>
<?php print ' <div id="bubble_quote"> <p>' . $quote . '</p> </div>' ?>
<?php endif; ?>
...and it works perfectly!
I want to do the same if an image is uploaded (i.e. it only prints the image if an image is uploaded).
I modified the above code, but it doesn't work - the html for the image is printed even if the image is not uploaded:
<?php $genericbubble = $node->field_genericbubble[0]['view'] ?>
<? if (isset($genericbubble)) : ?>
<?php print '<div id="bubble_photo_outerwrapper"> <div id="bubble_photo_innerwrapper"> <div id="bubble_photo_frame"> </div> <div id="bubble_photo">' . $genericbubble . '</div></div></div>' ?>
<?php endif; ?>
(If you are wondering why there are so many DIV wrappers, it is because I'm trying to overlay a background image over another image).
Why should it work for quotes, but not images?
Any help would be appreciated.
Comments
Comment #1
Anonymous (not verified) commentedNo codiong expert here , maybe this will help with ideas though
if i only want a field present when there is content in the field i use this
if i want a field that is looking for an image to exist on the server, i use something like this
Comment #2
big_smile commentedI only want it to print the image field if an image has been uploaded.
If no image has been uploaded, then I don't want it print anything (i.e. no image, no error message, and none of the HTML tags that go with the image).
I don't understand why my code doesn't work, as it works for the quote (see first code block in original post)!
Comment #3
jrglasgow commentedthe first thing for you to do is paste in your
$nodevariable in<code>tags so I can take a look at your available variables.Comment #4
big_smile commentedHi jrglasgow, Thank you greatly for your help!
I'm still a bit of a noob, so I do not know what you mean by $node variable.
However, I have attached a couple of files which may help:
1) The contents of my contemplate in a txt file (contemplate.txt)
2) The contents of the body variaibles that appear when I use contemplate (bodyvariables.txt)
3) the contents of page.tpl.php file in a txt file (page-tpl-php.txt)
Thanks for any help you can offer ^_^
Comment #5
big_smile commentedI solved this and thought I would post the solution for any body else who has the same problem.
It should be:
$genericbubble = $node->content['field_genericbubble']['field']['items'][0]['#children'];The reason is that field_genericbubble[0]['view'] applies even if content has not been uploaded.
content['field_genericbubble']['field']['items'][0]['#children'] refers to the actual uploaded content.
Comment #7
xalexas commentedI just want to add something if someone need slightly different approach with Imagecache module:
if(isset($node->field_yourfieldname[0])):endif;This way you can print image with imagecache preset and check if image is uploaded.
Note for Drupal starters:
imagecachepresetname means you have to write image cache preset name that you want to use to display image.
field_yourfieldname means you have write name of your field defined in content types. Don't just copy/paste this code and expect it to work.