Quantcast
Viewing all articles
Browse latest Browse all 10

Magento Backoffice (Admin Panel) Options - [Part 3]

In the previous part (Magento Backoffice (Admin Panel) Options - [Part 2]) we created a module and added some code to manage the module from "Admin Panel". A new menu entry is now responsible for modifications of the module settings. But Magento allows us to use a different approach.

Every module can contain a configuration file named system.xml. In this file we can define tabs and sections which will be later placed in System->Configuration, we define their content also. Data entered in the sections is stored in core_config_data table. Handling the data between the sections and the database in done by Magento, so it's a quite handy solution if we just need to put the data inside the database.

First, we need to create system.xml in module etc directory. The file should contain following definitions: what will be the tab for our module management, what sections it will contain and what is the content of each section. Our file is quite simple. First, the tabs:

<tabs>
    <settime>
        <label>Set Time</label>
        <sort_order>100</sort_order>
    </settime>
</tabs>

Every tab has its name, label which will be displayed, and number corresponding to the order of all tabs collection. Next we define sections, which can be compared to submenus of the tab:

<sections>
    <setit>
    </setit>
</sections>

And then we put inside sections their content:

<class>separator-top</class>

Section label:

<label>Set It!</label>

Name of the tab containing the section:

<tab>settime</tab>

Data type:

<frontend_type>text</frontend_type>

Sort order - important if there is more than one section within a tab:

<sort_order>40</sort_order>

Should the section be visible when we define default configuration:

<show_in_default>1</show_in_default>

Should the section be visible when we define configuration for a website:

<show_in_website>1</show_in_website>

Should the section be visible when we define configuration for a store:

<show_in_store>1</show_in_store>

Time for section content. Groups are containers for similar data fields:

<groups>
    <settingtime>
        <label>Set Time</label>
        <frontend_type>text</frontend_type>
        <sort_order>100</sort_order>
        <show_in_default>1</show_in_default>
        <show_in_website>1</show_in_website>
        <show_in_store>1</show_in_store>
        <fields>
            <time_format>
                <label>Time Format</label>
                <comment>string according to PHP date() argument</comment>
                <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>
            </time_format>                       
        </fields>
    </settingtime>
</groups>

So now system.xml looks like that:

<config>
    <tabs>
        <settime>
            <label>Set Time</label>
            <sort_order>100</sort_order>
        </settime>
    </tabs>
    <sections>
        <setit>
            <class>separator-top</class>
            <label>Set It!</label>
            <tab>settime</tab>
            <frontend_type>text</frontend_type>
            <sort_order>40</sort_order>
            <show_in_default>1</show_in_default>
            <show_in_website>1</show_in_website>
            <show_in_store>0</show_in_store>
            <groups>
                <settingtime>
                    <label>Set Time</label>
                    <frontend_type>text</frontend_type>
                    <sort_order>100</sort_order>
                    <show_in_default>1</show_in_default>
                    <show_in_website>1</show_in_website>
                    <show_in_store>1</show_in_store>
                    <fields>
                        <time_format>
                            <label>Time Format</label>
                            <comment>string according to PHP date() argument</comment>
                            <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>
                        </time_format>                       
                    </fields>
                </settingtime>
            </groups>
        </setit>
    </sections>
</config>

But we need something more to make it work - modification in config.xml. We need to add to it information about configuration resources that are related to our module.

<acl>
    <resources>
        <admin>
            <children>
                <system>
                    <children>
                        <config>
                            <children>
                                <setit>
                                    <title>Setit Section</title>
                                </setit>
                            </children>
                        </config>
                    </children>
                </system>
            </children>
        </admin>
    </resources>
</acl>

As you can see, we define path to our section. The path will be also relevant for saving or loading data from database, because it is used as an identifier. In our case data entered in field time_format wil have identifier setit/settingtime/time_format.

Magento will add the sections to permissions management in System->Permissions->Roles->Role Resources. Check this tab, our new sections should be listed there when you choose 'Custom'. If you see 'Access Denied' error when you click on 'Set It!' section, then go to System->Permissions->Roles, select Administrators and Role Resources. Change All to Custom, mark all checkboxes - be careful, because mistake in this step may cause inability to login to Admin Panel. Save, then go back and change Custom back to All and save again. Now you should have fully functional module management.

Here is a screenshot of final result in Magento back office:

Image may be NSFW.
Clik here to view.
Set it screenshot


Viewing all articles
Browse latest Browse all 10

Trending Articles