addField类型图像和缩略图路径

发布于 2024-09-04 02:58:23 字数 623 浏览 2 评论 0原文

我有一个 Magento 网上商店,刚刚创建了一个带有扩展名 (Modulecreator) 的自定义模块。该模块带有一个标准的管理界面,可以处理文件上传。我发现,如果您想显示缩略图,可以使用“图像”字段类型 (addField('myfield', 'image')) 而不是“文件”类型字段。

但现在我遇到了问题。当我上传文件时,我将它们保存在“media”目录中名为“slides”的子目录中。但是当我编辑该项目时,上传字段旁边的缩略图的图像路径设置为“媒体”文件夹。不是我的文件夹,因为我将其设置为“media/slides/”。

我使用以下代码:

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

我尝试在数组中设置“路径”键,但这不会被 Magento 拾取。讨厌 Magento 缺乏对良好且易于使用的文档的支持...

也许你可以帮助我找到解决方案?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

熊抱啵儿 2024-09-11 02:58:23

其他解决方案只是在 magento 调用 $form->setValues(...) 后编辑您的路径
基本上在返回之前,做这样的事情:

  $p = $form->getElement('filename')->getValue();
  $form->getElement('filename')->setValue('media/slides' . $p);

.gondo

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

子栖 2024-09-11 02:58:23

好的,我找到了解决我的问题的方法。我更改了创建“图像”字段类型的文件。该文件位于/lib/Varien/Data/Form/Element/Image.php。当您查看代码时,您会看到最后一个函数是从给定的数据数组中获取键“名称”:

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

所以我想如果我创建另一个函数来获取“路径”索引会怎样:

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

接下来,当您使用函数 _getUrl 时(),获取的是上传图片的预览URL,需要修改如下(只需在'$this->getValue()'之前添加函数getPath()):

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

我认为这不是最解决这个问题的优雅方法,但它满足了我对该项目的需求。我希望我的解决方案对其他人也适用,如果他们也面临这个问题......

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

又爬满兰若 2024-09-11 02:58:23

没错,这不是解决这个问题的最优雅的方法,但它确实有效;)。

一种更优雅的方法是在您自己的模块中创建您自己的图像字段类型。
我已经在 StackOverflow 帖子中解释了执行此操作的方法: 这里

我希望它能帮助您以更优雅的方式做到这一点;)

Hugues。

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.

剑心龙吟 2024-09-11 02:58:23

嗯,当我刚开始从事 magento 模块开发工作时,我曾经遇到过这个问题。
使用 Varien_File_Uploader 将为您提供已保存文件的路径及其名称(使用索引“文件”)。
因此,在将信息保存到数据库之前,请在数组中创建一个索引(使用表中的列名)(在控制器保存操作中提交表单时保留 Post 数据),

例如:

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

保存操作后,它肯定会将文件路径信息存储到表中相应的列。
希望有帮助。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文