addField type image and thumbnail path
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?
other solution is just to edit your path after magento call $form->setValues(...) basically before return, do somehting like this:
$p = $form->getElement('filename')->getValue();
$form->getElement('filename')->setValue('media/slides' . $p);
.gondo
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...
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.
hmm i had this issue once when i just started working on magento module dev. using Varien_File_Uploader will give you the path of a saved file with its name (using the index 'file'). so before saving the info in database, create an index (use column name in the table) in an array (keeping Post data when form submits in controller save action)
for example:
$upload = new Varien_File_Uploader($_FILE['custom_image']['name']);
$p = $upload->save('in/to/the/folder');
$data = $this->getRequest()->getPost();
$data['custom_image'] = 'myfolder'. DS .'subfolder'.$p['file'] // path return after save through Varien_File_Uploader.
after save operation it will definitely store the file path info in to its respective column in a table. Hope it helps.
精彩评论