Apr 15, 2012
kalpesh

Magento: Difference between source_model, frontend_model, backend_model

When dealing with Magento Admin’s system configuration, you may have encountered with these models. If you don’t know, these models are used when you add custom fields through your system.xml file at Magento’s backend configuration (Admin -> System -> Configuration).

Okay, so the difference between these 3 models are:

source_model:
It specifies a Model class, where you will be returned with options that can populate in the current field.

Example,

<source_model>adminhtml/system_config_source_yesno</source_model>

this will populate select dropdown with values Yes/No

frontend_model:
It specifies a Block class to add custom render to use as a frontend field type instead of/along with default frontend_type field value (text, textarea, select, etc.).

Example,

<frontend_type>button</frontend_type>
<frontend_model>MyCustomModule/button</frontend_model>

this will keep frontend type as button, but the customized one taken from Block class Button.php of MyCustomModule module

Code at Button.php can be anything like this:

<?php 
class MyNamespace_MyCustomModule_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;
    }
}
?>

backend_model:
Generally, when you save any form at System Configuration, the data is stored in the table core_config_data. But if your requirement is to do something prior/after the save is performed, you can do it by having _beforeSave() & _afterSave() methods defined in this custom/defined class.

Example,

<backend_model>adminhtml/system_config_backend_shipping_tablerate</backend_model>

2 Comments

  • Thanks this helped a lot! 😀

  • I tried example with your bluedart extension but it is giving error..

    Pkgname_Modulename_Block_Costweightrange’ not found in magento…path/app/code/core/Mage/Core/Model/Layout.php on line 590

    Actually I want to add extra fields in your bluedart package so, i tried this. I want to add 3 textboxes in a row something like

    Cost_______ for Weight From______ To ______

    in admin panel.

    “Block” directory was not there in your shipping pkd so i created it and specify the class name also in config.xml

Leave a comment

 

Welcome to my Blog

Kalpesh MehtaHelping Magento developers in their day-to-day development problems since 2011. Most of the problems and solutions here are my own experiences while working on different projects. Enjoy the blog and don't forget to throw comments and likes/+1's/tweets on posts you like. Thanks for visiting!

Certifications

Honor

Recognition

Magento top 50 contributors

Magento top 50 contributors

Contributions