Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

Custom module image field not displaying in admin form
 
mosquitodigital
Jr. Member
 
Total Posts:  1
Joined:  2012-09-27
 

I cannot workout how to correctly set the url for an image field on the edit form. I can create a new item and upload an image file to the following directory

$path = Mage::getBaseDir(’media’) . DS . ‘carousel’ . DS;

Yet when editing an item, the image field has the following url

http://magento.domain.local/media/addis-bins.jpg

instead of

http://magentosd.domain.local/media/carousel/addis-bins.jpg

How can I get the correct image url?

 
Magento Community Magento Community
Magento Community
Magento Community
 
tzyganu
Mentor
 
Avatar
Total Posts:  2205
Joined:  2009-11-18
Bucharest, Romania
 

Hello
in order to work with a custom path you need to write a custom renderer for the field.
Create this class:

class Company_Module_Block_Adminhtml_Entity_Helper_Image extends Varien_Data_Form_Element_Image{
    
protected function _getUrl(){
        $url 
false;
        if (
$this->getValue()) {
            $url 
=  Mage::getBaseUrl(’media’) . â€˜/carousel/’.$this->getValue();
        
}
        
return $url;
    
}
}
replace ‘Company’, ‘Module’ and ‘Entity’ to your own namespace, module name and entity name.
now you have to tell Magento to use your new renterer for image fields.

In the class ‘Company_Module_Block_Adminhtml_Entity_Edit_Tab_Form’ class (replace again the generic values with your own), assuming you have one, and you should, in the method _prepareForm() add this line:

$fieldset->addType('image''Company_Module_Block_Adminhtml_Entity_Helper_Image');
right before calling the first $fieldset->addField().
Replace again ‘company’…

In order for this to work make sure that your field is of type ‘image’ and not file.

if you want to avoid in the future this kind of problems try using this module creator:  https://github.com/tzyganu/moduleCreator
Details about if are found here:

http://marius-strajeru.blogspot.com/p/ultimate-module-creator.html
It’s still in beta and might have some bugs, but it saves you a lot of time.

Cheers,
Marius.

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top