0

I added some fields to core_config_data through system.xml of my module. as shown below:

        <groups>
            <hot_deals translate="label">
                <label>Hot deals</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>
                <fields>
                    <deals_category_id translate="label">
                        <label>Deals category id</label>
                        <frontend_type>text</frontend_type>
                        <show_in_default>1</show_in_default>
                        <show_in_website>1</show_in_website>
                        <show_in_store>1</show_in_store>
                    </deals_category_id>
                </fields>
            </hot_deals>
       </groups>

I want to apply some default value to the above created text field by changing the above system.xml file such that if user tried to empty the field and save then also the text field should hold the default value. In other cases like when the user altered the value, the new value should be store in the table.

Is it possible?

I can do this easily in phtml files by using if else statement but the problem is that there are many phtml files which need this change.

2

This can be achieved with a frontend model attached to the config.
So make the fields section look like this:

<fields>
    <deals_category_id translate="label">
        <label>Deals category id</label>
        <frontend_type>text</frontend_type>
        <backend_model>[module]/system_config_deal</backend_model>
        <show_in_default>1</show_in_default>
        <show_in_website>1</show_in_website>
        <show_in_store>1</show_in_store>
    </deals_category_id>
</fields>

Then create the file [Namepsace]/[Module]/Model/System/Config/Deal.php with the following content.

<?php 
class [Namepsace]_[Module]_Model_System_Config_Deal extends Mage_Core_Model_Config_Data {
    protected function _beforeSave()
    { 
         $value = $this->getValue(); //get the value sent through post
         if ($value === '') { //if the value is empty
              $this->setValue(YOUR DEFAULT VALUE GOES HERE); 
         }
    }
}

The method _beforeSave of the backend model will be called before inserting the value in the db so you can alter it as you want.

  • I haven't tried but I am sure it will work.. I did loop to all phtml files which I wasn't meant to do due to pressure.. btw, thanks :) – Mr_Green Apr 9 '14 at 13:38
  • Test it before saying thanks. I didn't test it either, I just saw that this is how it's done to add a slash at the end of the base url if the user forgets. – Marius Apr 9 '14 at 13:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.