How do you extend block capability to include thumbnails?

Permalink 1 user found helpful
I've had good luck so far writing (adapting, actually) my own simple blocks, but they've been limited to written information. Things like "name, price, description" in a restaurant menu.

But how do you add the capability to include a thumbnail image? So you would have instead, for instance, "name, price, thumbnail (clickable, so it pops up a larger view), description."

1db
 
MrNiceGaius replied on at Permalink Reply
MrNiceGaius
Designer Content
http://www.concrete5.org/marketplace/addons/designer-content/...

This FREE block will do everything you want... plus, once you build your custom block with it take a look at the newly created block code to see how it does it. Really awesome way to learn about custom blocks.

For quick and dirty code:

Here's n example of code I've used for my blocks view file:
<?php //display an image in a blocks view
$im = Loader::helper('image');
$full_img = File::getByID(123); //<--or however it is you're getting the images from the system
$maxWidth = 800; //or whatever
$maxHeight = 600; //or whatever
$img = $im->getThumbnail($full_img, $maxWidth, $maxHeight);
?>
<img src="<?php echo $img->src; ?>" width="<?php echo $img->width; ?>" height="<?php echo $img->height; ?>" alt="whatever" />


Depending on your block you may retrieve images from either the file manager by ID, which is saved in the blocks db.xml file or you design your block to display images that are stored in custom page attributes e.g. a blog roll with thumbnails.

For a block that has an image attribute for selecting an image on a per block basis you should really take a look at the Designer Content block, it will do everything you want and when you're finished just learn from the generated source code.

For a block that displays an image that is stored in a custom page attribute I can give you a recent example I've used to retrieve images from custom page attributes and display them on the page.

<?php //view.php
$c = Page::getCurrentPage();
$controller->showPostThumbnail( $c->getAttribute('preview_image') );
?>

<?php//controller.php
    public function showPostThumbnail($thumb){
      $im = Loader::helper('image');
            if(is_object($thumb)){
            $width = 600;
            $height = 400;            
            $im->outputThumbnail($thumb,$width,$height);
            }
    }
?>

There's a bit more to it than that, like setting up your block edit form & db.xml file (don't forget to refresh database tables after change under 'Add Functionality' in dashboard).

I've seen many different ways to do it so once you get the basics you'll find that it's very flexible.

Cheers,
Aaron
1db replied on at Permalink Reply
1db
I downloaded it. I installed it. I used it. Then I posted a 5-star review. Holy crap, this thing is sweet.
MrNiceGaius replied on at Permalink Best Answer Reply
MrNiceGaius
:) I know Jordanlev has good Karma ... maybe you can help my karma by marking my answer the best

... update ... I meant my answer with the code examples and explanation, grins ... oh well at least I got karma yay!!!!
jordanlev replied on at Permalink Reply
jordanlev
I may have good karma, but I don't have a sales team -- until now!
Thanks for the great explanation and sharing your own code. Have some karma :)