Question

I am Adding Custom Section in System->Config Tab.

I need to add Button as Field and its related action which I need to perform. But I cannot find on google for button all example provide other field option but hard to get button example.

Other Section Working fine but I am not able to add button and related action (filepath) which call on click.

<fields> 
  <goldprice translate="label"> 
    <label>GoldPrice</label>                   
    <frontend_type>text</frontend_type>                
    <sort_order>0</sort_order>  
    <show_in_default>1</show_in_default> 
    <show_in_website>1</show_in_website> 
    <show_in_store>1</show_in_store> 
    <comment>Gold Price per 1gm</comment> 
  </goldprice> 
  <run translate="label">
    <frontend_type>button</frontend_type>
    <frontend_model>/adminhtml_system_config_customfield</frontend_model>
    <sort_order>30</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>

My Config File Like Below

<?xml version="1.0"?>
<config>
  <modules>
    <LetsNurture_CustomGrid>
      <version>0.1.0</version>
    </LetsNurture_CustomGrid>
  </modules>
  <global>
    <blocks>
      <customgrid>
        <class>LetsNurture_CustomGrid_Block</class>
      </customgrid>
    </blocks>
    <models>
      <class>LetsNurture_CustomGrid_Model</class>
    </models>
    <helpers>
      <customgrid>
        <class>LetsNurture_CustomGrid_Helper</class>
      </customgrid>
    </helpers>
    <events>
      <admin_system_config_changed_section_gold> <!-- identifier of the event we want to catch -->
        <observers>
          <customgrid>
            <type>model</type>
            <class>LetsNurture_CustomGrid_Model_Observer</class>
            <method>UpdatePrice</method>
          </customgrid>
        </observers>
      </admin_system_config_changed_section_gold>
    </events>
  </global>
</config>

And Customfiled.php like below

class LetsNurture_CustomGrid_Block_Adminhtml_System_Config_Customfield extends Mage_Adminhtml_Block_System_Config_Form_Field
{
    protected function _getElementHtml(Varien_Data_Form_Element_Abstract $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;
    }
}

But it gives me Warning like this "failed to open stream: No such file or directory"

Thanks, Yogesh

Was it helpful?

Solution

<frontend_model> path goes to to Blocks (instead of Models).

Incidentally, even though it’s named a model class but we have to create it as a Magento Block class since this is how the other built in front end model classes are implemented.

As well as, it is used for template output so it’s appropriate to create a block class for this.

Example

<frontend_model>NAMESPACE_MODULENAME/adminhtml_system_config_customfield</frontend_model>

So

Create Customfield.php under .../NAMESPACE/MODULENAME/Block/Adminhtml/System/Config/ with the following content:

class NAMESPACE_MODULENAME_Block_Adminhtml_System_Config_Customfield extends Mage_Adminhtml_Block_System_Config_Form_Field
{


    protected function _getElementHtml(Varien_Data_Form_Element_Abstract $element)
    {       
        $html = parent::_getElementHtml($element);
       //Write your custom html here
        return $html;
    }
}

Hope this help you

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top