Ajouter des paramètres de configuration dans Magento

Voici un exemple de code montrant comment ajouter facilement ses propres paramètres de configuration dans Magento.

Aperçu

Une fois n’est pas coutume, on va afficher le résultat avant le code pour voir ce qu’il est possible d’ajouter dans la configuration Magento :

Code source

Contenu du fichier system.xml (dans le dossier etc/ de votre extension) :

<?xml version="1.0"?>
<config>
    <tabs>
        <myconf translate="label">
            <label>My Configuration</label>
            <sort_order>150</sort_order>
        </myconf>
    </tabs>
    <sections>
        <tab1 translate="label" module="adminhtml">
            <label>Tab #1</label>
            <tab>myconf</tab>
            <sort_order>10</sort_order>
            <show_in_default>1</show_in_default>
            <show_in_website>1</show_in_website>
            <show_in_store>1</show_in_store>
            <groups>
                <general translate="label comment">
                    <label>General</label>
                    <sort_order>50</sort_order>
                    <show_in_default>1</show_in_default>
                    <show_in_website>1</show_in_website>
                    <show_in_store>1</show_in_store>
                    <comment><![CDATA[This is a <strong>global comment</strong> about my <em>configuration</em>.<br />You can specify <u>custom html</u> tags. <a href="http://www.bubblecode.net/en" target="_blank">Click here for example!</a>]]></comment>
                    <fields>
                        <text_field translate="label comment">
                            <label>Text Field</label>
                            <comment>Text field with store view scope.</comment>
                            <frontend_type>text</frontend_type>
                            <sort_order>10</sort_order>
                            <show_in_default>1</show_in_default>
                            <show_in_website>1</show_in_website>
                            <show_in_store>1</show_in_store>
                        </text_field>
                        <textarea translate="label comment">
                            <label>Textarea</label>
                            <comment>Textarea with store view scope.</comment>
                            <frontend_type>textarea</frontend_type>
                            <sort_order>20</sort_order>
                            <show_in_default>1</show_in_default>
                            <show_in_website>1</show_in_website>
                            <show_in_store>1</show_in_store>
                        </textarea>
                        <dropdown translate="label comment">
                            <label>Dropdown</label>
                            <comment>Dropdown with global scope.</comment>
                            <frontend_type>select</frontend_type>
                            <source_model>jr_customconfigexample/system_config_source_dropdown_values</source_model>
                            <sort_order>30</sort_order>
                            <show_in_default>1</show_in_default>
                            <show_in_website>0</show_in_website>
                            <show_in_store>0</show_in_store>
                        </dropdown>
                        <multiple_dropdown translate="label comment">
                            <label>Multiselect</label>
                            <comment>Multiselect with global scope.</comment>
                            <frontend_type>multiselect</frontend_type>
                            <source_model>jr_customconfigexample/system_config_source_dropdown_values</source_model>
                            <sort_order>40</sort_order>
                            <show_in_default>1</show_in_default>
                            <show_in_website>0</show_in_website>
                            <show_in_store>0</show_in_store>
                        </multiple_dropdown>
                        <file translate="label comment">
                            <label>File</label>
                            <comment><![CDATA[File saved in <strong><span style="color: red;">var/uploads</span></strong> folder.]]></comment>
                            <frontend_type>file</frontend_type>
                            <backend_model>adminhtml/system_config_backend_file</backend_model>
                            <upload_dir>var/uploads</upload_dir>
                            <sort_order>50</sort_order>
                            <show_in_default>1</show_in_default>
                            <show_in_website>0</show_in_website>
                            <show_in_store>0</show_in_store>
                        </file>
                        <time translate="label comment">
                            <label>Time</label>
                            <frontend_type>time</frontend_type>
                            <sort_order>52</sort_order>
                            <show_in_default>1</show_in_default>
                            <show_in_website>1</show_in_website>
                            <show_in_store>1</show_in_store>
                        </time>
                        <active translate="label comment">
                            <label>Enable/Disable</label>
                            <frontend_type>select</frontend_type>
                            <sort_order>54</sort_order>
                            <source_model>adminhtml/system_config_source_enabledisable</source_model>
                            <show_in_default>1</show_in_default>
                            <show_in_website>1</show_in_website>
                            <show_in_store>1</show_in_store>
                        </active>
                        <heading_example translate="label">
                            <label>Heading example</label>
                            <frontend_model>adminhtml/system_config_form_field_heading</frontend_model>
                            <sort_order>55</sort_order>
                            <show_in_default>1</show_in_default>
                            <show_in_website>1</show_in_website>
                            <show_in_store>0</show_in_store>
                        </heading_example>
                        <boolean translate="label comment">
                            <label>Boolean</label>
                            <comment>Boolean with website scope and dependant fields when Yes is selected.</comment>
                            <frontend_type>select</frontend_type>
                            <source_model>adminhtml/system_config_source_yesno</source_model>
                            <sort_order>60</sort_order>
                            <show_in_default>1</show_in_default>
                            <show_in_website>1</show_in_website>
                            <show_in_store>0</show_in_store>
                        </boolean>
                        <dependant_text_field translate="label comment">
                            <label>Dependant Text Field</label>
                            <comment>This field depends of boolean value above.</comment>
                            <frontend_type>text</frontend_type>
                            <sort_order>70</sort_order>
                            <show_in_default>1</show_in_default>
                            <show_in_website>1</show_in_website>
                            <show_in_store>0</show_in_store>
                            <depends>
                                <boolean>1</boolean>
                            </depends>
                        </dependant_text_field>
                    </fields>
                </general>
            </groups>
        </tab1>
    </sections>
</config>

Contenu du fichier config.xml (dans le dossier etc/ de votre extension) :

<?xml version="1.0"?>
<config>
    <modules>
        <JR_CustomConfigExample>
            <version>0.1.0</version>
        </JR_CustomConfigExample>
    </modules>
    <global>
        <models>
            <jr_customconfigexample>
                <class>JR_CustomConfigExample_Model</class>
            </jr_customconfigexample>
        </models>
    </global>
    <default>
        <tab1>
            <general>
                <text_field>Default value</text_field>
                <textarea></textarea>
                <dropdown>key2</dropdown>
                <multiple_dropdown>key1,key2</multiple_dropdown>
                <time>11,30,45</time>
                <active>0</active>
                <boolean>1</boolean>
                <dependant_text_field>Default value</dependant_text_field>
            </general>
        </tab1>
    </default>
</config>

Contenu du fichier adminhtml.xml (dans le dossier etc/ de votre extension) :

<?xml version="1.0"?>
<config>
    <acl>
        <resources>
            <admin>
                <children>
                    <system>
                        <children>
                            <config>
                                <children>
                                    <tab1>
                                        <title>Tab #1</title> <!-- Utilisé dans l'arborescence des ressources -->
                                    </tab1>
                                </children>
                            </config>
                        </children>
                    </system>
                </children>
            </admin>
        </resources>
    </acl>
</config>

Configuration des valeurs des listes déroulantes :

<?php
 
class JR_CustomConfigExample_Model_System_Config_Source_Dropdown_Values
{
    public function toOptionArray()
    {
        return array(
            array(
                'value' => 'key1',
                'label' => 'Value 1',
            ),
            array(
                'value' => 'key2',
                'label' => 'Value 2',
            ),
        );
    }
}

Téléchargement

Le code source de l’extension est disponible ici. Il suffit de désarchiver à la racine de Magento, de vider le cache et enfin de vous déconnecter puis de vous reconnecter à l’admin.

36 réponses à “Ajouter des paramètres de configuration dans Magento”

  1. Bravo !

    C’est le module dans notre todolist à produire depuis maintenant trop longtemps !

    Obligé d’aller récupérer des parties de modules pour intégrer des fichiers, des listes déroulantes, etc.

    N’était pas visible sur le screen, avez-vous pensé à la config à plusieurs lignes, où il est possible de rajouter des options à la volée ? (comme les options d’un attribut par exemple)

    Frédéric MARTINEZ.

  2. jim dit :

    Is it possible to have sub-option dependent comments?
    Ie. only show a comment if an option is a certain value?
    Thanks :)

  3. Jean Paul dit :

    Voila!! merci pour votre post.

  4. angelo fraietta dit :

    Fantastic Tutorial

  5. Zeeshan Khalid dit :

    Hi Johan.
    Great tutorial works like a charm. Just one question. One your current tab1 if admin presses « Save config » I want all the data to be posted on an external link say http://www.test.com. How can i hook into Save Config and send data via AJAX.

  6. venkatesh dit :

    have a great code for easily understanding how to create modules tabs in magento. but here mage/source folder already created selected arrays available how we can include in our source model like languages in dropdown box try to give reply to this.i have tried but error showing.
    thank u.

  7. Mayank Patel dit :

    thanks i got good help….

  8. vipan kumar dit :

    Hello Everyone,

    Can anybody tell me how i can change tab1 under sections tag. i have tried just rename it from tab1 to tab2 and logout magento after save file and then check in admin configuration it still saying 404 page not found. please give me solutions

    Thanks in advance.

    
    
    
        
            
                My Configuration
                150
            
        
        
            
                Tab #1
                myconf
                10
                1
                1
                1
                
                    
                        General
                        50
                        1
                        1
                        1
                        <![CDATA[This is a global comment about my configuration.You can specify custom html tags. Click here for example!]]>
                        
                            
                                Text Field
                                Text field with store view scope.
                                text
                                10
                                1
                                1
                                1
                            
                        
                    
                
            
        
    
    
    
  9. Guillaume dit :

    Thank you very much ! This article is wonderful and very complete !

  10. ritz dit :

    what is the code to display content on frontend side? (means how to display image and all text fields values in front side?)

  11. Chintan dit :

    hi,

    well how can we add custom source model.
    it always gives me this error
    Call to a member function toOptionArray() on a non-object

  12. shiva dit :

    Hi

    How to get these values in product details page?
    Thanks

  13. Vishal dit :

    It is working fine.

  14. brian dit :

    I need help with magento. Is it possible to add a text box into the template that can be turned on and off through the admin. This is a shopping cart 3 column wide, I want the middle column at the top, just above the product. Let me know if possible.

  15. baraa dit :

    hi
    I need help with magento.
    i need add to data text field from table in database
    and i need save value text field by $value
    Thanks…..

  16. Sanjay Shankar dit :

    Hi,
    Thanks for the tutorial. But its not working for me. i am using Magento ver. 1.9.0.1 . Is there any issue with this version? Please let me know….

  17. Keval dit :

    Hi,
    Thank you for the extension. It’s work properly.
    I want to display a database value on text field in admin module.

    Thanks in advance…….

  18. Pradeep dit :

    subject :—- Including top links(login/logout sign up under Account as dropdown select values)

    I want to add login signup and login/out options inside account i.e account as dropdown menu where login , sign up and login/logout should be inside Account dropdown. Can please somebody guide me how to do this.. I tried but is default . .. Please help.. such that it should not effect the functionality . . . ..

    regards

  19. renish khunt dit :

    thank you so much for sharing.

  20. renish khunt dit :

    how can i get that value in front side please help.

  21. Vijay dit :

    Very nice tutorial it really helped…… :)

  22. lanrosta dit :

    This is an old post but it is still helpful. But, like many others who have asked previously, many folks need to know how to use the created field vars in their code. The author has left this part out and also has not responded to any of the questions regarding this aspect.

    Here’s how you do it (I got this info from here: http://www.demacmedia.com/magento-commerce/magento-tutorials/add-input-fields/) bottom of the page…

    Using Your Stored Config Values:

    Hopefully this additional info will help new visitors to implement their admin panel attribute variables into their code.

  23. lanrosta dit :

    didn’t post my code. grrr… here it is in php….

    // the value for your text field
    $text_field_value = Mage::getStoreConfig(‘NAMESPACE/GROUP_IDENTIFIER/TEXT_FIELD’);

    // the value for your yes/no (boolean) field
    $boolean_field_value = Mage::getStoreConfig(‘NAMESPACE/GROUP_IDENTIFIER/BOOLEAN_FIELD’);

  24. axl dit :

    nice tutorial, thanks…

  25. kumar harsh dit :

    how to add text field on selection of yes/no

  26. deepak dit :

    How to get image file on the frontend..?
    getStore());
    ?>

    <a href="getUrl( ») ?> »><img src=" »>

    but got this error:

    Access forbidden!

    You don’t have permission to access the requested object. It is either read-protected or not readable by the server.

    If you think this is a server error, please contact the webmaster.

    Error 403

  27. Priyank dit :

    Facebook Tab Timeline
    select
    2
    adminhtml/system_config_source_enabledisable
    1
    1
    1

    In this how can I set default enable in option?

  28. dhivya priya dit :

    Nice tutorial ! i installed magento 1.9.2.4 with sample data.How to view the text field and text area content in the home page.

  29. Jack Hendricks dit :

    Good tutorial. Setting up custom configs makes dealing with this bloated software bearable. Next best option to starting from scratch with a different framework.

  30. Dushyant Joshi dit :

    Thanks for sharing. works well.

  31. Maxime dit :

    Merci pour ce tuto. Je suis en train de faire un module Magento 1 et c’est juste ce que je cherchais.

Laisser un commentaire

* Champs requis

Categories