3

I have a custom module, through which I can upload multiple images from admin. Along with images, I have text, drop-down form fields in my module. Now I can upload all images to media folder of Magento and can store all form field information in the database. Now when editing my stored item from the grid page, I need to show all the images that correspond to the saved item in my module. How can achieve this? I think the code should come under editAction() in the controller. Any help or hint is highly appreciated. Thanks in advance.

My editAction() is given below. It displays already set form fields except for images while editing the item from the grid page.

<?php
public function editAction()
    {
        $bannerId   = $this->getRequest()->getParam('id');
        $bannerModel =   Mage::getModel('banner/banner')->load($bannerId);        
        if ($bannerModel->getId() || $bannerId == 0) 
        {
            Mage::register('banner_data', $bannerModel);
            $this->loadLayout();
            $this->_setActiveMenu('banner/items');
            $this->_addBreadcrumb(Mage::helper('adminhtml')->__('Banner Manager'), Mage::helper('adminhtml')
                ->__('Banner Manager'));
            $this->_addBreadcrumb(Mage::helper('adminhtml')->__('Banner Description'), 
                Mage::helper('adminhtml')->__('Banner Description'));
            $this->getLayout()->getBlock('head')->setCanLoadExtJs(true);
            $this->_addContent($this->getLayout()->createBlock('banner/adminhtml_banner_edit'))
                ->_addLeft($this->getLayout()->createBlock('banner/adminhtml_banner_edit_tabs'));

            $this->renderLayout();
        } 
        else 
        {
            Mage::getSingleton('adminhtml/session')->addError(Mage::helper('banner')->__('Banner does not exist'));
            $this->_redirect('*/*/');
        }
    }

EDIT Kindly note that I have two tabs for my module. Image uploading facility is provided in the second tab. I have used a phtml file for constructing that tab. So image upload field is provided thereby using <.input type="file" /> element. I need to display the uploaded images in this tab itself. So in effect, I need to add code in my phtml file in order to display these images.

2

You have to add the fields in _prepareForm() in \NameSpace\ModuleName\Block\Adminhtml\ModuleName\Edit\Tab\Form.php add the below code.

$fieldset->addField('<Data base Field name>', 'image', array(
        'label' => Mage::helper('<Module Name>')->__('Logo'),
        'name' => '<your Data base Field name>',
        'note' => '(*.jpg, *.png, *.gif)'
 ));
  • i have added the code through frontend. Means I have rendered a phtml file for appearing content of the tab(i have multiple tabs for my module).i have added the file fields using <input> element. <br/> eg:<input type="file" id="chain_Image_0" name="banner_image_0" class="input-text" multiple="multiple" /> – Rajeev K Tomy Jan 7 '14 at 0:54
  • please clear where you have to add image on back end or front end because I am not getting you .. – Keyul Shah Jan 7 '14 at 3:08
  • ok. i have two tabs for my module. i am using second tab for uploading images. i have used a phtml file for construct that tab. inside that phtml file, i have added file fields using input element of type file. I cant use your code directly in my phtml file. I think your code can use only when my tab is defined by addfield method (ie not using any phtml file ) – Rajeev K Tomy Jan 7 '14 at 3:15
  • did you get me. if not kindly provide me with chat link, so that i can explain it you in detail – Rajeev K Tomy Jan 7 '14 at 3:16
  • Yes I get you. let me do some R & D and let you know – Keyul Shah Jan 7 '14 at 3:19
1

To display image in custom admin grid you need to add a column in grid.php with following code:

$this->addColumn('Database_Field_Name', array(
          'header'    => Mage::helper('ModuleName')->__('Image'),
          'align'     =>'left',
          'type'      => 'image',
          'index'     => 'Database_Field_Name',
          'renderer' => 'modulename/adminhtml_modulename_renderer_image', //get the image HTML code class name in render class should be written in small caps
          'filter'    => false,
          'sortable'  => false,
));

After that you need to create following image renderer file on module's Block/Adminhtml/ModuleName/Renderer/Image.php location

<?php
class NameSpace_ModuleName_Block_Adminhtml_ModuleName_Renderer_Image extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract{
   public function render(Varien_Object $row)   {
        $html = '<img id="' . $this->getColumn()->getId() . '" src="'.Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA).$row->getData($this->getColumn()->getIndex()) . '"';
        $html .= '/>';
        return $html;
    }
}
?>
  • Buddy This code is useful for display image in magento admin grid but the question is not like that :) – Keyul Shah Jan 7 '14 at 17:53
0

Okay. I have found the solution for this. In phtml file, I get banner entity through registry and then used getData() method for accessing all of it's properties. ie

<?php
     /* registered the model to 'banner_data' through my controller(editAction())
       in the backend */
     $bannerModel = Mage::registry( 'banner_data' ); //gets model
     $bannerData = $bannerModel.getData(); //output an array that holds all fields of table
     $bannerPath = $bannerData[ 'img_path' ];
     $bannerImg = $bannerData[ 'image' ];        
?>

We can also use collection method also instead of this

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.