如何在系统配置中将“图像”字段添加到自定义动态字段中?


10

我想允许管理员用户生成他/她想要的尽可能多的字段。我在另一个扩展中找到了解决方案,并以此为起点。所以我有这样的代码:

system.xml

<showcases translate="label">
    <label>Showcases</label>
    <frontend_type>text</frontend_type>
    <sort_order>10</sort_order>
    <show_in_default>1</show_in_default>
    <show_in_website>1</show_in_website>
    <show_in_store>1</show_in_store>
    <fields>
        <showcase translate="label">
            <label>Showcases</label>
            <frontend_type>select</frontend_type>
            <frontend_model>awesomehome/adminhtml_showcases</frontend_model>
            <backend_model>adminhtml/system_config_backend_serialized</backend_model>
            <sort_order>410</sort_order>
            <show_in_default>1</show_in_default>
            <show_in_website>1</show_in_website>
            <show_in_store>1</show_in_store>
        </showcase>
    </fields>
</showcases>

Namespace/Awesomehome/Block/Adminhtml/Showcases.php

class Namespace_Awesomehome_Block_Adminhtml_Showcases 
    extends Mage_Adminhtml_Block_System_Config_Form_Field
{
    protected $_addRowButtonHtml = array();
    protected $_removeRowButtonHtml = array();

    protected function _getElementHtml(Varien_Data_Form_Element_Abstract $element)
    {
        $this->setElement($element);

        $html = '<div id="showcase_template" style="display:none">';
        $html .= $this->_getRowTemplateHtml();
        $html .= '</div>';

        $html .= '<ul id="showcase_container">';
        if ($this->_getValue('showcases')) {
            foreach (array_keys($this->_getValue('showcases')) as $row) {
                if ($row) {
                    $html .= $this->_getRowTemplateHtml($row);
                }
            }
        }
        $html .= '</ul>';
        $html .= $this->_getAddRowButtonHtml(
            'showcase_container',
            'showcase_template', $this->__('Add new showcase')
        );

        return $html;
    }

    protected function _getRowTemplateHtml($row = 0)
    {
        $html = '<li><fieldset>';

        $html .= $this->_getShowcaseTypeHtml($row);

        $html .= $this->_getRemoveRowButtonHtml();
        $html .= '</fieldset></li>';

        return $html;
    }

    protected function _getShowcaseTypeHtml($row) {
        $html = '<label>' . $this->__('Showcase type:') . '</label>';

        $html .= '<select style="width:100%;" class="input-text" name="' . $this->getElement()->getName() . '[type][]">';
        $html .= '<option value="1" '
                . ($this->_getValue('type/' . $row) == "1" ? 'selected="selected"' : '') .'>'
                . $this->__("Simple") . "</option>";
        $html .= '<option value="2" '
                . ($this->_getValue('type/' . $row) == "2" ? 'selected="selected"' : '') .'>'
                . $this->__("With Image") . "</option>";

        $html .= '</select><br/>';
        return $html;
    }

它可以按预期工作,就像这样:

在此处输入图片说明

现在,我想将图像上传字段添加到我的字段集中。我该怎么办?

更新

我知道system.xml您可以编写以下代码来添加图像字段:

<image translate="label">
    <label>Image</label>
    <frontend_type>image</frontend_type>
    <backend_model>adminhtml/system_config_backend_image</backend_model>
    <upload_dir config="system/filesystem/media" scope_info="1">awesomehome/topcategories</upload_dir>
    <base_url type="media" scope_info="1">awesomehome/topcategories</base_url>
    <sort_order>30</sort_order>
    <show_in_default>1</show_in_default>
    <show_in_website>1</show_in_website>
    <show_in_store>1</show_in_store>
    <comment>Allowed file types: jpeg, gif, png.</comment>
</image>

但是我不能使用这种方法,因为我想拥有多个字段,而不是一个。

Answers:


2
Add this in your system.xml

<logo translate="label comment">
<label>Logo</label>
<comment>Allowed file types: jpeg, gif, png.</comment>
<frontend_type>image</frontend_type>
<backend_model>adminhtml/system_config_backend_image</backend_model>
<upload_dir config="system/filesystem/media" scope_info="1">theme</upload_dir>
<base_url type="media" scope_info="1">theme</base_url>
<sort_order>1</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
</logo>

元素表示将图像上传到的位置。在上面的示例中,图像将保存到媒体文件夹下的子文件夹中。例如/ media / theme /。该元素用于呈现标签。要从上述示例输出图像,可以使用以下代码

echo Mage::getBaseUrl('media') . Mage::getStoreConfig('system_config_name/group/logo');

我不能使用system.xml我的情况。请再次阅读我的问题。
Pedram Behroozi

但是为什么不能使用它
Vivek Khandelwal

因为您的方法将一个图像字段添加到系统配置中。我想拥有动态数量的图像字段。
Pedram Behroozi

好的。我会告诉您一种不同的方法
Vivek Khandelwal

1

我尝试了类似的方法,但只能部分解决。

首先,为了在您的array / serialized config选项中添加多种类型的字段,我创建了一个Mage_Adminhtml_Block_System_Config_Form_Field_Array_Abstract包含类型的类的扩展版本selectmultiselect并且file(由于原始功能仅允许您使用该text类型),请参见 https:/ /github.com/Genmato/Core/blob/master/app/code/community/Genmato/Core/Block/System/Config/Form/Field/Array/Abstract.php(此处包含的文件有点大)。

接下来,我发现将文件类型与其他(选择/文本)字段组合在一起无法正常工作。当保存数据时,只有文件详细信息可用,并且阵列混乱。因此,我选择了一种解决方案,即使用一个字段来保存上传内容:

<templates translate="label comment">
                            <label>Templates</label>
                            <frontend_model>genmato_addresslabel/system_config_form_templates</frontend_model>
                            <backend_model>genmato_addresslabel/system_config_backend_storefile</backend_model>
                            <sort_order>1</sort_order>
                            <show_in_default>1</show_in_default>
                            <show_in_website>0</show_in_website>
                            <show_in_store>0</show_in_store>
                            <upload_dir config="system/filesystem/media" scope_info="1">addresslabel</upload_dir>
                            <base_url type="media" scope_info="1">addresslabel</base_url>
                            <comment>Label templates, to be used as background in the PDF (only PDF files are allowed)</comment>
                        </templates>

对应的块类:

class Genmato_AddressLabel_Block_System_Config_Form_Templates extends Genmato_Core_Block_System_Config_Form_Field_Array_Abstract
{
    public function __construct()
    {
        $this->addColumn('template', array(
            'label' => Mage::helper('genmato_addresslabel')->__('Template'),
            'style' => 'width:300px',
            'class' => '',
            'type' => 'file',
        ));

        $this->_addAfter = false;
        $this->_addButtonLabel = Mage::helper('genmato_addresslabel')->__('Add new item');
        $this->setTemplate('genmato/core/system/config/form/field/array.phtml');
        parent::__construct();
    }

}

和后端模型类:

class Genmato_AddressLabel_Model_System_Config_Backend_Storefile extends Mage_Adminhtml_Model_System_Config_Backend_File
{

    protected function _afterLoad()
    {
        $value = (string)$this->getValue();
        $this->setValue(empty($value) ? false : unserialize($value));
    }

    protected function _beforeSave()
    {

        $value = $this->getValue();

        // Load current template data
        $data = unserialize(Mage::getStoreConfig($this->getPath()));

        // Check for deleted records
        if (is_array($data)) {
            foreach ($data as $key => $val) {
                if (!isset($value[$key])) {
                    unset($data[$key]);
                }
            }
        }

        // check for new uploads.
        foreach ($value as $key => $val) {
            if (!is_array($val)) {
                continue;
            }
            foreach ($val as $filefield => $filevalue) {
                try {
                    if ($_FILES['groups']['tmp_name'][$this->getGroupId()]['fields'][$this->getField()]['value'][$key][$filefield]) {
                        $file = array();
                        $tmpName = $_FILES['groups']['tmp_name'];
                        $file['tmp_name'] = $tmpName[$this->getGroupId()]['fields'][$this->getField()]['value'][$key][$filefield];
                        $name = $_FILES['groups']['name'];
                        $file['name'] = $name[$this->getGroupId()]['fields'][$this->getField()]['value'][$key][$filefield];

                        if (isset($file['tmp_name']) || empty($file['tmp_name'])) {
                            $uploadDir = $this->_getUploadDir();

                            $uploader = new Mage_Core_Model_File_Uploader($file);
                            $uploader->setAllowedExtensions($this->_getAllowedExtensions());
                            $uploader->setAllowRenameFiles(true);
                            $result = $uploader->save($uploadDir);

                            $filename = $result['file'];
                            if ($filename) {
                                if ($this->_addWhetherScopeInfo()) {
                                    $filename = $this->_prependScopeInfo($filename);
                                }

                            }
                            $data[$key]['template'] = $filename;
                        } else {

                        }
                    }

                } catch (Exception $e) {
                    Mage::throwException($e->getMessage());
                    return $this;
                }
            }
        }

        $this->setValue(serialize($data));

        return $this;
    }

}

第二个字段用于存储配置:

<config translate="label comment">
                            <label>Label configurations</label>
                            <frontend_model>genmato_addresslabel/system_config_form_config</frontend_model>
                            <backend_model>genmato_pdflib/system_config_backend_storeserial</backend_model>
                            <sort_order>2</sort_order>
                            <show_in_default>1</show_in_default>
                            <show_in_website>0</show_in_website>
                            <show_in_store>0</show_in_store>
                            <comment>Label configuration, you can create multiple label configurations for different usages</comment>
                        </config>

和使用的块类:

class Genmato_AddressLabel_Block_System_Config_Form_Config extends Genmato_Core_Block_System_Config_Form_Field_Array_Abstract
{
    public function __construct()
    {

        $this->addColumn('name', array(
            'label' => Mage::helper('genmato_addresslabel')->__('Name'),
            'style' => 'width:100px',
        ));

        $this->addColumn('template', array(
            'label' => Mage::helper('genmato_addresslabel')->__('Use template'),
            'style' => 'width:100px',
            'type' => 'select',
            'options' => Mage::getModel('genmato_addresslabel/system_config_source_templates')->getTemplates(),
        ));

        $this->addColumn('width', array(
            'label' => Mage::helper('genmato_addresslabel')->__('W (mm)'),
            'style' => 'width:40px'
        ));

        $this->addColumn('height', array(
            'label' => Mage::helper('genmato_addresslabel')->__('H (mm)'),
            'style' => 'width:40px'
        ));

        $this->addColumn('rotate', array(
            'label' => Mage::helper('genmato_addresslabel')->__('Rotate 90'),
            'type' => 'select',
            'options' => array("0" => "No", "1" => "Yes"),
            'style' => 'width:50px'
        ));

        $this->addColumn('multiple', array(
            'label' => Mage::helper('genmato_addresslabel')->__('Multiple labels'),
            'type' => 'select',
            'options' => array("0" => "No", "1" => "Yes"),
            'style' => 'width:50px'
        ));

        $this->addColumn('label_width', array(
            'label' => Mage::helper('genmato_addresslabel')->__('W (mm)'),
            'style' => 'width:40px'
        ));

        $this->addColumn('label_height', array(
            'label' => Mage::helper('genmato_addresslabel')->__('H (mm)'),
            'style' => 'width:40px'
        ));

        $this->_addAfter = false;
        $this->_addButtonLabel = Mage::helper('genmato_addresslabel')->__('Add new item');
        $this->setTemplate('genmato/core/system/config/form/field/array.phtml');
        parent::__construct();
    }

}

在这里,我使用选择/下拉选项在每个配置行中选择上载的文件,这也使我可以在多个行中使用相同的文件。

这可能不是您所处情况的完美解决方案,但可能是解决问题的起点。随意使用Genmato_Core(请参阅https://github.com/Genmato/Core)模块中使用的部分代码作为您自己的解决方案。


谢谢。我今天将尝试,并让您知道。看起来很有希望。
Pedram Behroozi

@PedramBehroozi您尝试过了吗,奏效了吗?我也会有兴趣:)
simonthesorcerer 2015年

@simonthesorcerer还没有,但是我应该在星期六之前处理它。即将更新。
Pedram Behroozi 2015年

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.