views:

201

answers:

2

Hello,

I have a Magento webshop and just created a custom module with the extension (Modulecreator). This module comes with a standard admin interface that can handle file uploads. I found out that if you want to show thumbnails you can use the 'image' field type (addField('myfield', 'image')) instead of the 'file' type field.

But now i've got a problem. When I upload files, I save them in a subdirectory called 'slides' in the 'media' directory. But when I edit the item, the image path for the thumbnail next to the uploadfield is set to the 'media' folder. Not my folder as I set it to 'media/slides/'.

I use the following code:

$fieldset->addField('filename', 'image', array(
        'label'     => Mage::helper('slideshow')->__('File'),
        'required'  => true,
        'name'      => 'filename',
        ));

I tried to set a 'path' key in the array, but this doesn't get picked up by Magento. Hate the lack of support for good and easy to use documentation by Magento...

Maybe you can help me out to find a solution?

+1  A: 

Ok, I found a solution for my problem. I changed the file that creates the 'image' field type. The file is located at /lib/Varien/Data/Form/Element/Image.php. When you look at the code you see the last function is to get the key 'name' from the given data array:

public function getName()
{
    return  $this->getData('name');
}

So I thougth what if i make another function to get a 'path' index:

public function getPath()
{
    return  $this->getData('path');
}

Next, when you go the function _getUrl(), which get's the preview URL for the uploaded image, you need to change it the following (only add the function getPath() before '$this->getValue()'):

protected function _getUrl()
{
    return $this->getPath().$this->getValue();
}

I don't think this is the most elegant way to fix this, but it satisfies my needs for this project. I hope my solution is working for other people if they face this problem too...

Christiaan
A: 

Hello,

that's right that this is not the most elegant way to fix this, but it works ;).

A more elegant way would be to create your own image field type in your own module. I've explained the way to do that on a StackOverflow post : here.

I hope that it will help you to do that in a more elegant manner ;)

Hugues.

Hugues ALARY