Magento - 向system.xml添加一个按钮,并附加方法

Her*_*tin 13 magento

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

看起来"前端类型"按钮可能正如我所尝试的那样工作,它在配置部分添加了一个微小的可点击按钮.但是我无法在按钮本身上附加方法或标签.

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

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

有没有人知道如何:

  • 为按钮添加标签/值
  • 在按钮中添加一个类
  • 单击按钮时添加要调用的方法

非常感谢您的帮助 !

    <?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>
Run Code Online (Sandbox Code Playgroud)

liq*_*ity 18

注意:自从这个问题以来,Magento已经发展了.请注意,此解决方案可能无法在当前版本中使用.

你应该尝试添加一个<frontend_model></frontend_model>.例如 :

    <?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>
Run Code Online (Sandbox Code Playgroud)

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

<?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;
    }
}
?>
Run Code Online (Sandbox Code Playgroud)

感谢phy4me.

为了更好地了解发生了什么阅读的核心来源:app/code/core/Mage/Adminhtml/Block/System/Config/Form.phpinitForm()功能和initFields()功能.

雨果.

编辑:我删除了大写编辑:更正了拼写错误


Her*_*tin 5

Hugues的回答很有效.需要注意的一点是,frontend_model操作不能有限制.

这一定是

<frontend_model>bar/button</frontend_model>
Run Code Online (Sandbox Code Playgroud)

代替

<frontend_model>Bar/Button</frontend_model>
Run Code Online (Sandbox Code Playgroud)

所以这就是我在整个管理过程中完成所有工作的方法.

1)Hugues声明的后续指示(再次注意不要在frontend_model调用中加上大写字母)

2)在app/code/local/Foo/Bar/Block/Button.php中,更改了$ url定义,使其调用Foo_Bar模块的管理控制器

$url = $this->getUrl('bar/adminhtml_controller/action');
Run Code Online (Sandbox Code Playgroud)

3)创建/编辑了Foo_Bar管理控制器的动作,我在其中调用了所需的方法

Mage::getModel('bar/block')->method();
Run Code Online (Sandbox Code Playgroud)

并添加了一个重定向到我希望用户重定向到的adminhtml区域(在我的情况下配置的运营商部分):

$this->_redirect('adminhtml/system_config/edit/section/carriers');
Run Code Online (Sandbox Code Playgroud)

一切都在流动!

再次感谢 ...!