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 upload path
 
stefkaye
Jr. Member
 
Total Posts:  23
Joined:  2012-10-22
 

Hi,

I’ve created a custom module with an image upload feature. However within the admin I get a broken image next to the upload button. How do I change the path from:

http://localhost/dev/media/image.jpg

to

http://localhost/dev/media/customslider/image.jpg

Thanks

Stefan

 
Magento Community Magento Community
Magento Community
Magento Community
 
jordipuig
Jr. Member
 
Total Posts:  12
Joined:  2013-05-21
 

Hi,
When you load the form info, you should modify the loaded value to the url you want:

if(Mage::registry('object_data')){
            $form
->setValues(Mage::registry('object_data')->getData());
            
$p $form->getElement('imageName')->getValue();
            
$form->getElement('imageName')->setValue('/subfolder/'.$p);
}
 
Magento Community Magento Community
Magento Community
Magento Community
 
Magerts
Jr. Member
 
Avatar
Total Posts:  4
Joined:  2013-03-31
 

Hi,
For that look for the form.php that should be into the module->Block->adminhtml->Modulename->Edit->Tab->Form.php
Make changes into the data that are being fetched from the database.

if(Mage::registry('module_data'))
{
                    $form
->setValues(Mage::registry('module _data')->getData());
                    
$myform $form->getElement('ImageFieldName')->getValue();
                    
$form->getElement(' ImageFieldName ')->setValue('/IMageFolderPath/'.$ myform );
}

Hope this will help you.

 
Magento Community Magento Community
Magento Community
Magento Community
 
stefkaye
Jr. Member
 
Total Posts:  23
Joined:  2012-10-22
 

Spot on guys, worked a treat, Thanks.

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

Hello.
The cleanest way to do it, is to have a custom renderer for the file input field.
Let’s say your module is called Easylife_Press and you need this image input file for your entity called article.
You need this file: app/code/local/Easylife/Press/Block/Adminhtml/Helper/Article/Image.php:

<?php
class Easylife_Press_Block_Adminhtml_Article_Helper_Image extends Varien_Data_Form_Element_Image{
    
protected function _getUrl(){
        $url 
false;
        if (
$this->getValue()) {
            $url 
Mage::getBaseUrl('media').'customslider/'.$this->getValue();
        
}
        
return $url;
    
}
}
and in your edit block add this line right after defining the $fieldset variable:
$fieldset->addType('image'Mage::getConfig()->getBlockClassName('press/adminhtml_article_helper_image'));
The line above will tell Magento to use your class as the renderer for image fields.

Marius.

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