I'm trying to write a snippet that will check if an image has been loaded into a cck image field and if not then print a default image. I've been having a look at the user image code in the user module to try get some clues but am a bit stuck on how to apply the same principals to get what i need - i'm afraid this is my first attempt at php coding and i would really appreciate any help!

This is the section of code from the user module that performs the kind of thing i want but i'm not entirely sure how to adapt it:

function theme_user_picture($account) {
  if (variable_get('user_pictures', 0)) {
    if ($account->picture && file_exists($account->picture)) {
      $picture = file_create_url($account->picture);
    }
    else if (variable_get('user_picture_default', '')) {
      $picture = variable_get('user_picture_default', '');
    }

    if (isset($picture)) {
      $alt = t('%user\'s picture', array('%user' => $account->name ? $account->name : variable_get('anonymous', 'Anonymous')));
      $picture = theme('image', $picture, $alt, $alt, '', false);
      if (!empty($account->uid) && user_access('access user profiles')) {
        $picture = l($picture, "user/$account->uid", array('title' => t('View user profile.')), NULL, NULL, FALSE, TRUE);
      }

      return "<div class=\"picture\">$picture</div>";
    }
  }
}

and this is the section of code in my cck template that printed out my news image:

    <?php foreach ((array)$field_interragate_main_image as $item) { ?>
      <div class="field-item"><?php print $item['view'] ?><?php print $field_interragate_main_image[0]['title'] ?></div>
    <?php } ?>

I'm guessing i need some kind of if else statement that would say - any help on the structuring would be greatly appreciated as i want to learn how to do this properly so i can start learning to code and contribute to drupal:

 foreach ((array)$field_interragate_main_image as $item) {
if $field_interragate_main_image is true print $item['view'], $field_interragate_main_image[0]['title'];
else print $picture; 

Comments

Dublin Drupaller’s picture

here's a snippet I used in a cck tpl.php file to display nothing if no WEBURL field was set with the node. You could try tweaking the fieldnames to do the same for the imagefield and/or display a default image if none is set.

<?php if ($field_weblink['view']) { ?> 
<p><strong>Weblink:</strong> <a href="<?php print $field_weblink['view'] ?>" target="_blank"><?php print $field_weblink['view'] ?></a></p>
<P>(Weblinks open in a new window)</p>
<?php }; ?>

please post back up here if you have success as I think others will find it useful.

Dub

Currently in Switzerland working as an Application Developer with UBS Investment Bank...using Drupal 7 and lots of swiss chocolate

catch’s picture

I'm trying to have no photo printed if there's no picture to avoid broken images, although in contemplate rather than a tpl.php file for now.

I modified your code to:

if ($field_photo['view']) {
print $field_photo['view']
};

but didn't have any luck. Will keep tring though and report back if I get anywhere.

stanbroughl’s picture

this should work for you as you only want to print out the image if it exists - it works fine on my teasers, i'm just trying to work out how to apply imagecache as well:

<?php foreach ((array)$field_photo as $item) { ?>
<div class="field-item"><?php print $item['view'] ?></div>
<?php } ?>

i have this for my news teaser - which prints the image if there is one and then from character number X the first X number of characters i specify from a field, which in my case is the story text:

<div class="news-teaser">
    <?php foreach ((array)$field_main_image as $item) { ?>
      <div class="field-item"><?php print $item['view'] ?></div>
    <?php } ?>
  </div>
<?php print $teaser = substr($field_story_text[0]['value'], 0, 300) ?><a href="<?php print $path ?>">...readmore</a></div>

I've not managed to provide a default picture if nothing is uploaded yet and will post here if and when i do

catch’s picture

Doh, I was using this code originally and a couple of people in IE were reporting broken images - I realise now that was because my imagcache tests weren't working, nothing to do with this at all!

However, an unexpected bonus of posting on this thread is the custom character limit from your teaser template. I was originally forcing a character limit on my introduction field in CCK which was an ugly way to do it, this is much better!

stanbroughl’s picture

i found it on another post and it works so much better than any of the teaser modules - its great! i'm going to check the handbooks and see if anyone has put it in there yet as this is one that needs to be shared!

Have you got imagecache working yet? i'm still having no luck

catch’s picture

Just got scale working, no crop yet.

although I'm still getting internet explorer errors with the working code. I think it needs an if statement somewhere.

posted up details here: http://drupal.org/node/78579