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

时间:2010-06-16 14:53:59

标签: 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>

2 个答案:

答案 0 :(得分: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>

然后创建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;
    }
}
?>

感谢phy4me。

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

雨果。

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

答案 1 :(得分:5)

Hugues的回答成功了。 需要注意的一点是,frontend_model操作不能有限制。

这一定是

<frontend_model>bar/button</frontend_model>

而不是

<frontend_model>Bar/Button</frontend_model>

所以这就是我在整个管理过程中所做的工作。

1)按照Hugues的说明进行操作(再次注意不要在frontend_model调用中设置上限)

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

$url = $this->getUrl('bar/adminhtml_controller/action');

3)创建/编辑了Foo_Bar管理控制器的操作,我用

调用了所需的方法
Mage::getModel('bar/block')->method();

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

$this->_redirect('adminhtml/system_config/edit/section/carriers');

一切都在流动!

再次感谢...!