Theo Cerutti
Theo Cerutti

Reputation: 970

How to create a custom system field in magento 1?

Magento 1.9

I want to create a new tab in System > Configuration.
In this tab, I need a group tab and in this group tab i want a textarea which is connected to a field of my database. If i edit my textarea, it will modify my database field too.

Look at this: https://prnt.sc/orwph1

I don't know how to connect my textarea with my db.. Create a new tab with a new group it's easy but connect it to the db..

Thanks!

Upvotes: 0

Views: 1658

Answers (1)

Marco Acquati
Marco Acquati

Reputation: 81

let's assume you want a section that has 2 fields: multiselect, text area. In the multiselect you have all your customer and in the text are you get the modify to apply for a certain db field (related to the customer).

your system.xml should me something like this:

<config>
<tabs>
    <admin_customTab_2 translate="label" module="sections">
        <label>- TAB NAME -</label>
        <sort_order>2</sort_order>
    </admin_customTab_2>
</tabs>

<sections>
    <customsection2 translate="label" module="sections">
        <label>label name</label>
        <tab>admin_customTab_2</tab>
        <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>
        <groups>
            <block translate="label">
                <label>label name</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>
                <fields>
                    <block_customers>
                        <label>Select user</label>
                        <comment>user list</comment>
                        <frontend_type>multiselect</frontend_type>
                        <backend_model>sections/users</backend_model>
                        <source_model>sections/users</source_model> <!-- adding a source-model for the form's select -->
                        <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>
                    </block_customers>
                    <block_textarea>
                        <label>changes to apply</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>
                    </block_textarea>
                </fields>
            </block>
        </groups>
    </customsection2>
</sections>

this is not enough for the configs, you need to tell Magento about the access control list (ACL) or admin-users will not see it. It is done in the config.xml like this:

    <adminhtml>
    <acl>
        <resources>
            <admin>
                <children>
                    <system>
                        <children>
                            <config>
                                <children>
                                    <customsection2>
                                        <title>Customer Changes?</title>
                                    </customsection2>
                                </children>
                            </config>
                        </children>
                    </system>
                </children>
            </admin>
        </resources>
    </acl>
</adminhtml>

all is setted, but doing nothing but showing us the tabs and forms. If you was brave enough you noticed a tag in the system.xml code up here,

<backend_model>sections/users</backend_model>
<source_model>sections/users</source_model> <!-- adding a source-model for the form's select -->

this tell magento that you are using a model relating to this form, where you can do operations. So, in your Model's path create the (in this example) User.php and here we go:

you want to extend your custom class with this one. class Admin_Sections_Model_Users extends Mage_Core_Model_Config_Data

yes but we still didn't insert any data in the form. You can do this by just adding a special function toOptionArray() :

    public function toOptionArray()
{
    $collections = Mage::getModel("customer/customer")->getCollection();

    foreach ($collections as $colletion){
        $user = Mage::getModel("customer/customer")->load($colletion->getId());
        $array[] = array('value'=>$user->getEntityId(),'label'=>Mage::helper("sections")->__($user->getName()));
    }
    return $array ;
}

this will set ALL the customer's name into the multi-select form.

"yeah, nice.. I asked about the text-area" before telling you how to get Data from Store Configs, you should know that there are 2 main methods for elaborating the form data:

    public function _afterSave()
{}
    public function _beforeSave()
{}

in there you can put all your code, no need to explain what is the difference between them.

in this function you can easy get the Data from Store Config by doing:

$text_area_string = Mage::getStoreConfig('customsection2/block/block_textarea');

"yeah nice... but my main problem is to save things in DB"

you can use whenever you prefer the Mage::getModel('') method , this can connect you to the database. Lets make an example; modify the 'is_active' field on 'customer_entity' table.

    $customer = Mage::getModel("customer/customer")->load($customer_id);
    $customer->setData("is_active",0);
    $customer->save();

this field doesn't do really anything, it is just a very fast example. This should let you do what you are aiming for. If something sounds strange, please let me know!

Upvotes: 1

Related Questions