Hide blog thumbnail block if no image

Permalink
Hi guys, I have posted about this before and have had no reply so I've had no choice but to post about it again as the issue has risen its ugly head once again.

I'm using the premade blog_index_thumbnail.php template as my news page. This uses a block to show the thumbnail image, which is added via the composer. I have very little PHP knowledge and have no clue how using this block differs from attributes and which is better, but as it's there I'm using it.

The code looks like this:
<div class="image-link">
  <a <?php  if ($target != '') { ?> target="<?php echo $target?>" <?php  } ?> href="<?php echo $nh->getLinkToCollection($cobj)?>">
  <?php 
  $ts = $cobj->getBlocks('Thumbnail Image');
  if (is_object($ts[0])) {
    $tsb = $ts[0]->getInstance();
    $thumb = $tsb->getFileObject();
    if($thumb){
      $imgHelper->outputThumbnail($thumb, 800, 400, $title);
    } 
  }
  ?></a>
</div><!-- end image-link -->


My issue is, when there is no image added in the composer, that code still tries to show an image that isn't there, so I'm getting a broken image/border. I need an if statement around the whole thing to hide if there is no image added. Can anyone help?

gracehcoote
 
gracehcoote replied on at Permalink Reply
gracehcoote
Can no one help me with this at all??
fudyartanto replied on at Permalink Reply
fudyartanto
You already try use image helper..?
there is example
<?php
$ih = Loader::helper('image');
//from your code try this one
$thumb = $tsb->getFileObject(); 
if(is_object($thumb)){
    $imgThumb = $ih->getThumbnail($thumb, 800, 400, $title);
}
?>
<?php if(isset($imgThumb) && is_object($imgThumb)):?>
<img src="<?php echo $imgThumb->src?>" alt="<?php echo $title?>">
<?php endif;?>


So you only print the image if image exist
gracehcoote replied on at Permalink Reply
gracehcoote
Thank you for replying!

I tried your code but I just get an error. There is already code at the start of the file to load the image helper, so I changed your code to match accordingly:

<?php 
   defined('C5_EXECUTE') or die("Access Denied.");
   $textHelper = Loader::helper("text");
   $imgHelper = Loader::Helper('image');
   $dateHelper = Loader::Helper('date');
   $isFirst = true;
   $thumb = $tsb->getFileObject(); 
   if(is_object($thumb)){
       $imgThumb = $imgHelper->getThumbnail($thumb, 800, 400, $title);
   }
</php>


However I am getting this error:

Fatal error: Call to a member function getFileObject() on a non-object in /home/mastercraft/public_html/concrete/blocks/page_list/templates/blog_index_thumbnail.php on line 9

This is line 9:
$thumb = $tsb->getFileObject();


The rest of the page is blank. Any ideas what I'm doing wrong?
fudyartanto replied on at Permalink Reply
fudyartanto
sorry, your $tsb var not assign any value.. this is complete example from your code

<div class="image-link">
  <a <?php  if ($target != '') { ?> target="<?php echo $target?>" <?php  } ?> href="<?php echo $nh->getLinkToCollection($cobj)?>">
  <?php 
  $ih = Loader::helper('image');
  $ts = $cobj->getBlocks('Thumbnail Image');
  if (is_object($ts[0])) {
    $tsb = $ts[0]->getInstance();
    $thumb = $tsb->getFileObject();
    if(is_object($thumb)){
      $imgThumb = $ih->getThumbnail($thumb, 800, 400, false);
      echo is_object($imgThumb) ? "<img src='{$imgThumb->src}' alt=''/>" : "";
    } 
  }
  ?></a>
</div><!-- end image-link -->


if still not work, upload your complete code here.. I'll try to help
gracehcoote replied on at Permalink Reply
gracehcoote
Thank you so much for your help on this - I'm totally lost on it tbh.

I have tried your new code and various different iterations of it but with no success, so here's the complete code for that page:

<?php 
   defined('C5_EXECUTE') or die("Access Denied.");
   $textHelper = Loader::helper("text");
   $imgHelper = Loader::Helper('image');
   $dateHelper = Loader::Helper('date');
   $isFirst = true;
   /* @var $dateHelper DateHeler */
   // now that we're in the specialized content file for this block type, 
   // we'll include this block type's class, and pass the block to it, and get
   // the content
   if (count($cArray) > 0) { ?>
   <?php 
   for ($i = 0; $i < count($cArray); $i++ ) {
      $cobj = $cArray[$i]; 
      $target = $cobj->getAttribute('nav_target');
gracehcoote replied on at Permalink Reply
gracehcoote
Tried again with some parts from your code but still showing the image even if one hasn't been added. Used this code:

if (is_object($ts[0])) {
    $tsb = $ts[0]->getInstance();
    $thumb = $tsb->getFileObject();
    if(isset($thumb) && is_object($thumb)){
        $imgHelper->outputThumbnail($thumb, 800, 400, $title);
    } 
}
gracehcoote replied on at Permalink Reply
gracehcoote
Having found no solution for this that works, I've just had to use jQuery to sort the issue (and the same for another similar issue). In this case, even though no image is found, the <img> tag is still used, so I did a check to see if the src attribute was empty and if so to hide the parent element. Ended up with the following code:

$('img.ccm-output-thumbnail').each(function() {
  if($(this).attr("src") == "") {
    $(this).parent().parent().hide();
  }
});