Home » Magento1 May 2011

Magento: Add default value to System Configuration Option fields

This article shows how to add default value to any system configuration option fields. System configuration option fields means those options under System -> Configuration.

Suppose, you have the following system.xml file. System.xml file is necessary to add admin configuration options.

system.xml

<?xml version="1.0" encoding="UTF-8"?>
    <config>
       <sections>         
    		<mysection translate="label" module="mymodule">
    			<label>My Section</label>
    			<tab>catalog</tab>
    			<frontend_type>text</frontend_type>
    			<sort_order>110</sort_order>
    			<show_in_default>1</show_in_default>
    			<show_in_website>1</show_in_website>
    			<show_in_store>1</show_in_store> 
    			<groups>
    				<mygroup translate="label" module="mymodule">
    					<label>My Group</label>
    					<frontend_type>text</frontend_type>
    					<sort_order>99</sort_order>
    					<show_in_default>1</show_in_default>
    					<show_in_website>1</show_in_website>
    					<show_in_store>1</show_in_store>
    					<fields>
    						<myfield translate="label comment">
    							<label>My Field</label>							
    							<frontend_type>text</frontend_type>
    							<sort_order>1</sort_order>
    							<show_in_default>1</show_in_default>
    							<show_in_website>1</show_in_website>
    							<show_in_store>1</show_in_store>
    						</myfield>						
    					</fields>
    				</mygroup>
    			</groups>			
    		</mysection>			
        </sections>
    </config>

Now, to add default value to the admin configuration options, you need to write the following in config.xml file of your module. The should be inside config node.

config.xml

    <default>
    	<mysection>
    		<mygroup>                
    			<myfield>My Default Value</myfield>			
    		</mygroup>		
    	</mysection>
    </default>

Hope this helps. Thanks.

From Mukesh Chapagain's Blog, post Magento: Add default value to System Configuration Option fields

php magento mukesh chapagain

Get New Post by Email

Find me on

Facebook Twitter Google+ LinkedIn RSS Feed
  • Access3000

    Hi,

    thanks for sharing your knwoledge. Do you also know how to set up default values for options (select fields) in an adminhtml form. Is it possible?

  • Access3000

    Ok,

    i have answered my own question.

    You can do it like this in your Form.php:
            $myObject = new Varien_Object();
            // set object data to data of model, stored in registry
            $myObject->setData(Mage::registry(‘model_data’)->getData());

            // place this code right before you add your field with addField()
            // set default value for field product_status
            if ( ! $myObject->hasProductStatus()) {  $myObject->setProductStatus(Mage_Catalog_Model_Product_Status::STATUS_ENABLED);
            }

  • Khamar_pratik

     Enabled
     select
     adminhtml/system_config_source_yesno
     1
     1
     1
     1
     

    This is my code and add in custom module to enable disable module in system.xml but didn’work pls help ….