Image block default image from attribute?

Permalink
Hi.
I'm building a product image scroll like this
http://olle.dyndns-ip.com/wasa/vara-produkter/knackebrod/...

It's a page-list custom template.
Images and descriptions is taken from an image page attribute when creating the pages. So I can get the images i the page list by something like
$img = $page->getAttribute('product_image');
$thumb = $ih->getThumbnail($img, 250, 350, false); and then
<li><img src="<?php  echo $thumb->src ?>" /></li>


Clicking the front image takes me to a detail page.
http://olle.dyndns-ip.com/wasa/vara-produkter/knackebrod/runda-grov...
Image in that page is taken from the same attribute and displayed in the page like so

if($c->getAttribute('product_image')) {
    $ih = Loader::Helper('image');
    $icon = $ih->getThumbnail($c->getAttribute('product_image'), 372, 372);
    echo '<img class="product-image" alt="test" src="'. $icon->src .'"  />';
    }


Now all that is fine. But what if I want to have that image into a image block? So I can style i a little bit with Concrete design options? Or maybe change the image to a different image than the one from the'product_image' attribute.
Is there a way that i can put any default content into the image block?
So that image block have the same image as i'm getting from the getAttribute('product_image')

I had a look at the Image block to make a custom template for it but my Php skills are rather limited.

I woulds appreciate any pointers or help.

Regards
olle

 
jordanlev replied on at Permalink Reply
jordanlev
There are ways to retrieve the contents of a specific block on a page, but it might be easier to use the free Page List Teasers addon (http://concrete5.org/marketplace/addons/page-list-teasers... ). Install that addon, then set your page list block's custom template to "Teasers", then edit the block and set its "truncate" option so that the box is checked but the number of characters is empty or 0 -- now the page list will show the first block in the 'Main' area of each page. Now just add an image block as the first block in each page's 'Main' area and you're good to go.
olleka replied on at Permalink Best Answer Reply
Hi
Thank you. I see what you mean. I really don't need the 'product_image' attribute if I do like you suggest.

But I actually managed to make a custom template for the image block.
I made a "templates" folder inside the image block. (I did copy the block from "concrete->blocks" first). Then I made a copy of the view.php and with this code in it.
<?php 
defined('C5_EXECUTE') or die("Access Denied.");
$imgHelper = Loader::helper('image');
global $c;
if ($c->getAttribute('product_image')) {
   $image = $imgHelper->getThumbnail($c->getAttribute('product_image'), 350, 350);
echo '<img class="product-image2" alt="test" src="'. $image->src .'"  />';
}?>


The key to get it working was the "global $c;" i guess?
In the theme page template i did not need the global $c;.
Don't know why :) But it works.

Regards
Olle
jordanlev replied on at Permalink Reply
jordanlev
Glad you got it figured out.

The reason you don't need to declare $c yourself in theme templates is because the C5 system does that for you (behind the scenes, so you don't actually see it happening). But it doesn't do that for blocks, so you need to bring it in yourself (either with "global $c;", or with "$c = Page::getCurrentPage();").