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

Anonymous’s picture

No 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

<?php 
foreach ((array)$field_translations as $item) {
if(!empty($item['view'])&& $item['view'] != " <br />"){
print $item['view'] ;
}}
?>

if i want a field that is looking for an image to exist on the server, i use something like this

 if(!file_exists($mylocation/.$myfile)){
        **report missing image** 
         }
 else {
**do something with image**
        }
big_smile’s picture

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

jrglasgow’s picture

the first thing for you to do is paste in your $node variable in <code> tags so I can take a look at your available variables.

big_smile’s picture

StatusFileSize
new3.11 KB
new21.74 KB
new638 bytes

Hi 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 ^_^

big_smile’s picture

Status: Active » Fixed

I 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.

Status: Fixed » Closed (fixed)

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

xalexas’s picture

I just want to add something if someone need slightly different approach with Imagecache module:

if(isset($node->field_yourfieldname[0])):

              
              $img = theme('imagecache', 'imagecachepresetname', $field_yourfieldname[0]['filepath']);
              print $img;
              
              

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.