Add a button to system.xml with a method attached

How to add a button to system.xml with method attached to it?
Add a button to system.xml with a method attached Image

On your system.xml:

<?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>

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;
    }
}
?>

For example if you need like I that the link open in a new window I used:

->setOnClick("window.open('$url')")
// instead of:
->setOnClick("setLocation('$url')")

If you need more flexibility you can use the following example, it's a bit harcoded but you can take it as example.

First the system.xml:

<cache_flush_products>
    <label>Flush JSON Products Cache</label>
    <frontend_type>button</frontend_type>
    <frontend_model>refinery29_json/flush</frontend_model>
    <sort_order>4</sort_order>
    <show_in_default>1</show_in_default>
    <show_in_website>1</show_in_website>
    <show_in_store>1</show_in_store>
    <flush_tag>Products</flush_tag>
    <flush_url>rf29json/product/get/cache/flush</flush_url>
</cache_flush_products>

And then the custom block:

<?php
class Refinery29_Json_Block_Flush extends Mage_Adminhtml_Block_System_Config_Form_Field
{
 
    protected function _getElementHtml(Varien_Data_Form_Element_Abstract $element) {
        $this->setElement($element);
        $fromStore = $element->getScopeId();
        $layout = $this->getLayout();
        $flushTag = $element->getOriginalData('flush_tag');
        $flushUrl = Mage::getUrl($element->getOriginalData('flush_url'));
        $backUrl = Mage::helper('adminhtml')->getUrl('system_config/edit/section/refinery29_json');
        $button = $layout->createBlock('adminhtml/widget_button')
            ->setType('button')
            ->setClass('scalable')
            ->setLabel('Flush Cache')
            ->setOnClick('javascript:executeFlush'.$flushTag.'();');
        $buttonHTML = $button->toHtml();
        $jsFunction = '
        <script type="text/javascript">
        function executeFlush'.$flushTag.'()
        {
            if(confirm("Are you sure?")) {
                new Ajax.Request("'.$flushUrl.'",{
                    method: "get",
                    onSuccess: function(transport){
                        if (transport.responseText){
                            alert("Cache flushed.");
                        }
                    }
                });
            }
        }
        </script>';
        $html = $layout->createBlock('core/text','flush-button')->setText($jsFunction.$buttonHTML)->toHtml();
        return $html;
    }
 
}

Leave your comment

* Required Fields