Custom pagelist template - if no image is set for a page, a thumbnail displays but is the previous page's thumb

Permalink
HI There,

I have a custom pagelist template which pulls a thumbnail from a custom attribute - this works great when an image is selected.

However, if a page doesn't have an image chosen, it seems to display the thumbnail from the previous page.

Here's the code which grabs the image from the custom attribute:
<!-- entry thumbnail -->
   <p><a <?php echo $target ?> href="<?php echo $link ?>"><?php echo "<img src=\"{$thumbnailObj->src}\" alt=\"\" width=\"240\" />"; ?></a><p>


Is there a way to not display an image if one isn't set?

Any help would be much appreciated.

Cheers

Ben

 
beebs93 replied on at Permalink Reply
beebs93
<?php
$c = Page::getCurrentPage();
$objFile = $c->getAttribute('thumbnail_attr_handle_here');
$strPath = NULL;
if(is_object($objFile) && $objFile instanceof File && !$objFile->error){
   $strPath = $objFile->getVersion()->getRelativePath();   
}
$htmImg = !is_null($strPath) ? '<img src="' . $strPath . '" width="123" height="456" alt="Alt text here" />' : '<span>Fallback HTML here</span>';
?>
<!-- entry thumbnail -->
<p><a <?php echo $target; ?> href="<?php echo $link; ?>"><?php echo $htmImg; ?></a><p>


I'm not sure on your setup, but you may want to modify this so if the page attribute isn't set that the entire link is not shown, but that's up to you.

Hope this helps :)
cmscss replied on at Permalink Reply
Thanks for that - the only php I know has been picked up working with c5 so it's fairly crappy sorry.

I have this call in the first php block at the top of the page:
// load image helper
   $imageHelper = Loader::helper("image");
   // load page attribute 'project_main_image' and apply thumbnail size
   $thumbnail = $cobj->getAttribute('main_image');
      if ($thumbnail) {
         $thumbnailObj = $imageHelper->getThumbnail($thumbnail, 240, 155);
      }


Then I thought I'd try to use a conditional when outputting the image in the markup:
<!-- entry thumbnail -->
   <?php
      if   ($thumbnail != "") { 
            echo '<a $target href="$link"> "<img src=\"{$thumbnailObj->src}\" alt=\"\" width=\"240\" />"</a>'; 
      }
      else {
         echo '<p></p>';
      }
   ?>


But because I've copied and pasted form other examples, I think my syntax is messed up.

BTW, is a conditional the right way to handle the duplicate image issue?

Cheers

Ben
beebs93 replied on at Permalink Reply
beebs93
Ah ok, you want to generate the thumbnail from an image - sorry, I missed that part.
You're pretty much there, but it just needs a bit tidying up.

You definitely want some sort of conditional logic instead of assuming anything. I even go so far as to check that even if the page attribute is set, that it's the right kind of data. There's nothing stopping you/someone else from changing the page attribute's type from a file to ordinary plain text. If that were to happen you could potentially generate a fatal PHP error that could be displayed to the general public, or, if your site is in production mode, screw things up without you knowing why.

Also, skip using the PHP block - just put this directly in your template file to keep everything together.

<?php
$objThumb = NULL;
// Get the file object of the image assigned to your page attribute (if any)
$objFile = $cobj->getAttribute('image_attr_handle_here');
// If it's a valid file object...
if(is_object($objFile) && $objFile instanceof File && !$objFile->error){
   // ...load the image helper and generate its thumbnail
   $ih = Loader::helper('image');   
   $objThumb = $ih->getThumbnail($objFile, 123, 456);
}
// If we successfully got a valid thumbnail we write out an HTML <img> tag with the appropriate attributes
if(!is_null($objThumb)){
   $strHtml = '<img src="' . $objThumb->src . '" width="' . $objThumb->width . '" height="' . $objThumb->height . '" alt="Alt text here" />';
}else{
   // Handle your fallback here (eg. Text, a hardcoded default image, etc)
cmscss replied on at Permalink Reply
Thanks heaps for the reply - the thumbnails now display correctly, but can the markup be rendered only if an image is set?

i.e. I'd like this markup to appear only if an image is set - otherwise nothing:
<!-- main product image -->
<div class="unit size2of5">   
<p><a <?php echo $target; ?> href="<?php echo $link; ?>"><?php echo $strHtml; ?></a><p>
</div>


Hope that makes sense - each time I try to echo the markup into the if statement itself, I get syntax errors.

Cheers

Ben
jordanlev replied on at Permalink Reply
jordanlev
Change this line in beebs93's code:
$strHtml = '<span>Fallback HTML here</span>';

...to this:
$strHtml = '';
cmscss replied on at Permalink Reply
Thanks for the reply,

I tried your suggestion but because the final markup is rendered outside the if statement (at the bottom of beebs93's code), the output is rendered even if no image is set - it just renders it unparsed like this:
<!-- entry thumbnail -->
<p><a <?php echo $target; ?> href="<?php echo $link; ?>"><?php echo $strHtml; ?></a><p>


I think I need to move the above markup into the if conditional. Then leave the else one blank which would render nothing when no image is set - is that right?

The problem is that I run into syntax errors when I try moving the markup into the if statement. I tried removing the extra <?php calls but realised I have no idea what I'm doing.

Sorry if I'm missing something simple.
jordanlev replied on at Permalink Reply
jordanlev
Please post your entire template file -- it's getting very difficult to tell what's going on exactly :)

(Note that the forum software prevents attaching files that end in .php, so either ZIP it up or change the extension to .txt)
cmscss replied on at Permalink Reply 1 Attachment
Thanks mate,

The page_list template is attached. The code which renders the markup is located further down on line 65.
beebs93 replied on at Permalink Reply
beebs93
Ok, I *think* I get it. As you're looping through the list of pages, if a thumbnail has NOT been set you're wanting to skip said page and continue to the next one? If so:

// If it's a valid file object...
if(is_object($objFile) && $objFile instanceof File && !$objFile->error){
   // ...load the image helper and generate its thumbnail
   $ih = Loader::helper('image');   
   $objThumb = $ih->getThumbnail($objFile, 240, 155);
}else{
   continue;
}


Let me know if I'm getting warmer...

P.S. On line 61, change:

<h4><a <?php echo $target ?> href="<?php echo $link ?>"><?php echo $title ?></h4></a>


to

<h4><a <?php echo $target ?> href="<?php echo $link ?>"><?php echo $title ?></a></h4>


as you've got your closing tags backwards.
cmscss replied on at Permalink Reply
Thanks for that.

I don't want to skip the page, I just don't want the image markup appearing so the excerpt text slides over.

You can see what's happening on this page:http://dev.sciasciabrothers.co.nz/steelpipe/index.php/projects/...

As you scroll down, you'll notice some entries don't have images but there's a hole for one. That's because the markup is getting rendered even when no image is set.

What I would like is for the markup to not render if no image is set - so that the text slides over. I'm sure I've done this on a c5 site but I can't find the markup.

From memory, I moved the if statement to where the markup is being rendered so that it contains the <div class="unit unit size2of5"> markup.

But each time I do, I get syntax errors.

Hope this makes sense!
jordanlev replied on at Permalink Reply 1 Attachment
jordanlev
Try this (see attached)
beebs93 replied on at Permalink Reply
beebs93
Ok, I get it now. Starting from line 29:

<?php
//IMAGE THUMBS
$objThumb = NULL;
// Get the file object of the image assigned to your page attribute (if any)
$objFile = $cobj->getAttribute('main_image');
// If it's a valid file object...
if(is_object($objFile) && $objFile instanceof File && !$objFile->error){
   // ...load the image helper and generate its thumbnail
   $ih = Loader::helper('image');   
   $objThumb = $ih->getThumbnail($objFile, 240, 155);
}   
?>
<div class="group project">   
   <h4><a <?php echo $target ?> href="<?php echo $link ?>"><?php echo $title ?></h4></a>
   <!-- entry thumbnail -->
cmscss replied on at Permalink Reply
Thanks guys - much appreciated.

But neither example actually renders the thumbnail - I see the div and link markup but no image.

Is this the bit that's supposed to render the image?
<?php echo $strHtml; ?>
beebs93 replied on at Permalink Best Answer Reply
beebs93
I modified my last example - I accidently snipped out the part that actually echoed the <img> thumbnail
cmscss replied on at Permalink Reply
Thanks for that works great!

If I could mark both answers as best I would - in the end I understood beebs93 answer more so have chosen his.

Thanks heaps for both your answers - very much appreciated.

Cheers

Ben
jordanlev replied on at Permalink Reply
jordanlev
That's cool -- he needs the karma more than me ;)
(Also he put in more effort here explaining it -- nice work!)

Glad you were able to solve the problem.

-Jordan
beebs93 replied on at Permalink Reply
beebs93
Glad you got it working :)

Plus, I agree with Jordan; he has enough karma to ensure he'll be at the cool table in the afterlife (the one where Johnny Cash, John Wayne and Johnny Carson sit) ;)
cmscss replied on at Permalink Reply
Nice...
pixelnpixel replied on at Permalink Reply
thanks it works:)