Magento - 向 system.xml 添加一个带有附加方法的按钮

标签 magento

我创建了一个模块,该模块具有定期运行的“类似导出”方法,如我的模块的 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>
    

    最佳答案

    注意:自从这个问题以来,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()功能。

    雨果。

    编辑:我删除了帽子
    编辑:纠正拼写错误

    关于Magento - 向 system.xml 添加一个带有附加方法的按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3054429/

    相关文章:

    php - 如何在 magento 1.7 中显示产品描述的所见即所得编辑器,而不在弹出窗口中显示它?

    php - Magento,帮助 SQL 查询获取完整的产品 URL

    php - 如何开始和回滚数据库事务以包装用于 Magento 的 PHPUnit 套件

    Magento - 制作一个新的 getProductCollection() 函数

    php - 使用 PHP 生成 OAuth 1.0 签名

    magento - 新产品不会发生货币转换

    php - 无法在 wamp 服务器上安装 magento

    facebook - 更新谷歌搜索的主页/着陆页

    Magento 结帐步骤

    php - Magento 产品插入错误