Magento Themes and Magento Extensions

Adding Items To The Magento Admin Panel

Share

At this point, we have a working module. All of our functionality is in place, and our hard-coded values will work for a single instance of Magento. We have the equivalent of alocal module. The next step is to move these hard-coded values into the Magento admin panel, so they can be configured on multiple Magento instances without any code modification required.

It is quite straightforward to add items to the Magento admin panel system configuration, and it’s ideal for saving website-specific credentials or settings, like in our case the merchant ID and cookie timeout value.

Now, let’s log in to the Magento admin panel and navigate to System → Configurationusing the main menu. We can see a number of tabs on the left-hand side for configuring the various elements of our Magento instance, such as “General,” “Web,” “Design,” etc. We are now going to add a new tab for our module’s configuration items.

ADDING NEW SYSTEM CONFIGURATION TAB

Let’s create a new XML file calledapp/code/community/SmashingMagazine/Affiliate/etc/system.xml and add to it the following content:

<&quest;xml version="1.0" encoding="UTF-8"&quest;>
<config>

    <!-- we are defining a new tab -->
    <tabs>

        <!-- our tab unique short name -->
        <smashingmagazine>

            <!-- the title of our tab in the admin panel sidebar -->
            <label>Smashing Magazine</label>

            <!-- the order our tab should appear on the sidebar -->
            <sort_order>100</sort_order>

        </smashingmagazine>

    </tabs>

</config>

CONFIGURING ACL SETTINGS

Since we have added a new tab, we need to add a new entry to the Magento ACL (Access Control List) to allow access to this tab. Let’s create a new file calledapp/code/community/SmashingMagazine/Affiliate/etc/adminhtml.xml, with the following content:

<&quest;xml version="1.0" encoding="UTF-8"&quest;>
<adminhtml>
    <acl>
        <resources>
            <admin>
                <children>
                    <system>
                        <children>
                            <config>
                                <children>
                                    <smashingmagazine_affiliate>
                                        <title>Smashing Magazine
                                               Affiliate</title>
                                    </smashingmagazine_affiliate>
                                </children>
                            </config>
                        </children>
                    </system>
                </children>
            </admin>
        </resources>
    </acl>
</adminhtml>

Note: When making changes to the ACL, if you are already logged in to Magento you will have to log out and back in again for the new permissions to take effect. You may find you get a “404 page not found” when you try to access a newly-added tab that you do not have ACL access to.

ADDING NEW SYSTEM CONFIGURATION ITEMS

Next we will update our system.xml to add the items that we want to be configurable:

<&quest;xml version="1.0" encoding="UTF-8"&quest;>
<config>
    <tabs>
        <smashingmagazine>
            <label>Smashing Magazine</label>
            <sort_order>100</sort_order>
        </smashingmagazine>
    </tabs>

    <!-- we are adding a new section to our tab -->
    <sections>

        <!-- unique shortname for our section -->
        <smashingmagazine_affiliate>

            <!-- the title of our section in the sidebar -->
            <label>Affiliate Tracking</label>

            <!-- the tab under which we want our section to appear -->
            <tab>smashingmagazine</tab>

            <!-- order of section relative to our tab -->
            <sort_order>10</sort_order>

            <!-- visibility of our section -->
            <show_in_default>1</show_in_default>
            <show_in_website>1</show_in_website>
            <show_in_store>1</show_in_store>

            <!-- we are adding some new groups to our section -->
            <groups>

                <!-- the unique short code for our group -->
                <general>

                    <!-- the title of our group -->
                    <label>General Settings</label>

                    <!-- order of group relative to the section -->
                    <sort_order>10</sort_order>

                    <!-- visibility of our group -->
                    <show_in_default>1</show_in_default>
                    <show_in_website>1</show_in_website>
                    <show_in_store>1</show_in_store>

                    <!-- we are adding some new fields to our group -->
                    <fields>

                        <!-- the unique short code for our field -->
                        <status>

                            <!-- the label of our field -->
                            <label>Enabled</label>

                            <!-- the type of our field -->
                            <frontend_type>select</frontend_type>

                            <!-- the source of our 'select' type -->
                            <source_model>
                                adminhtml/system_config_source_enabledisable
                            </source_model>

                            <!-- order relative to the group -->
                            <sort_order>10</sort_order>

                            <!-- visibility of our field -->
                            <show_in_default>1</show_in_default>
                            <show_in_website>1</show_in_website>
                            <show_in_store>0</show_in_store>
                        </status>
                        <merchant_id>
                            <label>Merchant ID</label>
                            <frontend_type>text</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>
                        </merchant_id>
                    </fields>
                </general>
                <cookie>
                    <label>Cookie Settings</label>
                    <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>
                    <fields>
                        <timeout>
                            <label>Cookie Timeout</label>
                            <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>

                            <!--
                                we can add a comment that
                                will appear below the field
                            -->
                            <comment><![CDATA[
                                This is the amount of time
                                an affiliate cookie will last, in days
                            ]]></comment>
                        </timeout>
                    </fields>
                </cookie>
            </groups>
        </smashingmagazine_affiliate>
    </sections>
</config>

Comments are closed.