本文介绍了Magento - 将按钮添加到 system.xml 并附加方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个模块,它有一个export like"方法,它按照 config.xml 文件的模块的 cron 区域中的定义定期运行.但我想通过在系统配置中添加立即运行"按钮,从而使用 system.xml 文件,让用户能够按需运行此导出方法.

I have created a module that has an "export like" method running regularly as defined in my module's cron area of the config.xml file. But I'd like to give the user the ability to run this export method on demand by adding a "Run now" button in the system configuration, thus using the system.xml file.

似乎前端类型"按钮可能像我试过的那样工作,它在配置部分添加了一个可点击的小按钮.但是我无法在按钮本身上附加方法或标签.

It seems that the "frontend type" button may be working as I've tried and it adds a tiny clickable button in the config section. But I am not able to attach a method nor a label on the button itself.

我想在模块的Grid.php"文件中添加一个按钮,但这不是我想要做的,因为它适合我的 acl.

I thought about adding a button in the "Grid.php" file of the module but this not what I'd like to do as it does fit with my acl.

下面是我的 system.xml 文件,前端类型为按钮".

Below is my system.xml file with the "button" frontend type.

有没有人知道如何:

  • 为按钮添加标签/值
  • 给按钮添加一个类
  • 添加点击按钮时调用的方法

非常感谢您的帮助!

    <?xml version="1.0" encoding="UTF-8"?>
    <config>
     ...
         <fields>
          ...
          <run translate="label">
           <label>Run now</label>
           <frontend_type>button</frontend_type>
           <backend_model>SOME BACKEND MODEL</backend_model>
           <sort_order>20</sort_order>
           <show_in_default>1</show_in_default>
           <show_in_website>1</show_in_website>
           <show_in_store>1</show_in_store>
          </run>
         </fields>
...
    </config>

推荐答案

注意:从这个问题开始,Magento 已经发展.请注意,此解决方案可能不适用于当前版本.

您应该尝试添加.例如:

You should try to add a <frontend_model></frontend_model>.For example :

    <?xml version="1.0" encoding="UTF-8"?>
    <config>
     ...
         <fields>
          ...
          <run translate="label">
           <label>Run now</label>
           <frontend_type>button</frontend_type>
           <frontend_model>bar/button</frontend_model>
           <sort_order>20</sort_order>
           <show_in_default>1</show_in_default>
           <show_in_website>1</show_in_website>
           <show_in_store>1</show_in_store>
          </run>
         </fields>
...
    </config>

然后创建您将在其中复制的 app/code/local/Foo/Bar/Block/Button.php :

And then create app/code/local/Foo/Bar/Block/Button.php in which you wil copy :

<?php
class Foo_Bar_Block_Button extends Mage_Adminhtml_Block_System_Config_Form_Field
{

    protected function _getElementHtml(Varien_Data_Form_Element_Abstract $element)
    {
        $this->setElement($element);
        $url = $this->getUrl('catalog/product'); //

        $html = $this->getLayout()->createBlock('adminhtml/widget_button')
                    ->setType('button')
                    ->setClass('scalable')
                    ->setLabel('Run Now !')
                    ->setOnClick("setLocation('$url')")
                    ->toHtml();

        return $html;
    }
}
?>

感谢 phy4me.

为了更好地理解正在发生的事情,请阅读核心源代码:app/code/core/Mage/Adminhtml/Block/System/Config/Form.php initForm() 函数和 initFields() 函数.

To better understand what's happening read core sources : app/code/core/Mage/Adminhtml/Block/System/Config/Form.php the initForm() function and initFields() function.

雨果.

我去掉了大写更正了拼写错误

edit: I removed capsedit: corrected a spelling mistake

这篇关于Magento - 将按钮添加到 system.xml 并附加方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 11:56