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.
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.
Below is my system.xml file with the "button" frontend type.
Does anyone have a clue on how to :
Many thanks for your help !
<?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>
note: since this question, Magento has evolved. Be aware that this solution may not work in the current versions.
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>
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;
}
}
?>
Thank to phy4me.
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.
Hugues.
edit: I removed caps edit: corrected a spelling mistake
Hugues answer did the trick. One thing to watch out though is that the frontend_model action must not have caps.
This must be
<frontend_model>bar/button</frontend_model>
instead of
<frontend_model>Bar/Button</frontend_model>
So here is what I did to make the whole thing work in an admin-wide process.
1) Followed instructions stated by Hugues (once again, watch out not to put caps in the frontend_model call)
2) In app/code/local/Foo/Bar/Block/Button.php, changed the $url definition to make it call an admin controller of the Foo_Bar module
$url = $this->getUrl('bar/adminhtml_controller/action');
3) Created/edited the Foo_Bar admin controller's action in which I called the desired method with
Mage::getModel('bar/block')->method();
and added a redirect to the adminhtml area to which I wanted the user to be redirected (carriers section of config in my case) :
$this->_redirect('adminhtml/system_config/edit/section/carriers');
And everything flows !
Thanks again ...!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With