Search code examples
imagemagentoblockmagento-1.7frontend

Magento, show image on custom frontend block


I've a module up and running. On the backend side I can upload images with several attributes and I'm saving image path and other information on a custom table. On frontend I already managed to detect when I need to show these images. I've used an Observer that adds a new layout handle when the displayed product is found on the custom created table. Then on that layout handle I call a phtml template file and from here I call a function inside a block that will be in charge of doing all the checks to be sure wich image to show.

My problem is that I can not find how to show these images. Everything I found references how to add image tag on a phtml file doing some extra verifications on the inserted php code. Buy on my case everything is on php code outside any phtml file.

My phtml file code:

<div>
    <h3><?php $this->showCampaign(); ?></h3>
</div>

My block code for now:

<?php
class Dts_Banners_Block_Front extends Mage_Core_Block_Template {

    /**
     * Shows a campaign banner according to the current selected product
     * Seeks on main banners table to see if it is an image/html/product alone campaign
     * Once knows the campaign type search on needed table the needed fields
     */
    public function showCampaign() {
        //Zend_Debug::dump($this->getLayout()->getUpdate()->getHandles());

        $product = Mage::registry('current_product');
        $currentProdID = $product->getId();

        if (Mage::registry('campaign_data') && Mage::registry('campaign_data')->getId()) {
            $currentCampaign = Mage::registry('campaign_data');
        } else {
            return;
        }

        // get campaign type and show needed information  
        switch ($currentCampaign->getbanner_type()) {
            case 1: // image
                $myImgCollection = Mage::getModel('banners/bannersimg')->getCollection();
                $myImgCollection->addfieldtofilter('bannerid',$currentCampaign->getID());
                $currentImg = $myImgCollection->getFirstItem();
                $currentImgPath = $currentImg->getimg_path();
                //{{block type="catalog/product_new" product_id="16" template="catalog/product/view/your_new_page.phtml"}}
                break;
            case 2: // html
                echo "html";
                break;
            case 3: // plain product url
                echo "plain product url";
                break;
        }
    }
}

Solution

  • As usual, couple of hours searching for an answer and when I post the question, found the answer. Just in case someone needs it: is as easy as creating an image html tag inside the code and return it to the template file. Sample code:

    <?php
    class Dts_Banners_Block_Front extends Mage_Core_Block_Template {
    
        public function showCampaign() {
            ...
            $currentImgPath = $currentImg->getimg_path();
    
            //build html to output to the template  
            $html .= "<div class=\"visual\">";
            $html .= "<img src=\"" . $currentImgPath . "\" alt=\"\" />";
            $html .= "</div>";
            ....
            return $html;
        }
    

    And the phtml file code:

    <div class="visual">
        <?php echo $this->showCampaign(); ?>
    </div>