addField type image and thumbnail path

北慕城南 提交于 2019-12-05 19:20:13

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...

liquidity

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!