Magento 1 Admin Configuration – Set Path in form.php to View Image

adminconfigurationmodule

I am uploading images in my module using the folowing code at:

\Block\Adminhtml\Visualcolor\Edit\Tab\Form.php

$fieldset->addField('image', 'image', array(
  label' => Mage::helper('visualcolor')->__('Image'),
  'required' => false,
  'name' => 'image',
 ));

then I save it fine at the controller into my specific folder at media and works fine.

$path = Mage::getBaseDir('media') . DS . "visualcolors" . DS;
$uploader->save($path, $_FILES['image']['name']);

The problem it is how do i get the correct path in the form.php instead the default "media" folder?

Best Answer

You need a custom form element for the image field.
So create this file: app/code/local/[Namespace]/[Module]/Block/Adminhtml/Visualcolor/Helper/Image.php

class [Namespace]_[Module]_Block_Adminhtml_Visualcolor_Helper_Image
    extends Varien_Data_Form_Element_Image {
    protected function _getUrl(){
        $url = false;
        if ($this->getValue()) {
            $url = Mage::getBaseUrl('media').'visualcolors/'.$this->getValue();
        }
        return $url;
    }
}

now in your Block\Adminhtml\Visualcolor\Edit\Tab\Form.php file tell magento to use your block as the renderer for image fields.

Add this line of code right below $fieldset = $form->addFieldset(....);

$fieldset->addType('image', '[Namespace]_[Module]_Block_Adminhtml_Visualcolor_Helper_Image');
Related Topic