开发者

Magento: How can I have a “debug flag” in my Magento Extension?

I'd like to have a debug flag in the Magento extension that I'm developing. I'd like to have it set to true when I develop and when I release the extension to users have it removed (defaulting to false). What is the best way to implement this?

The first thing that comes to mind is to have this flag default to false and set it to true in my app/etc/local.xml file. But how can I introduce a new XML node into local.xml and have the Magento infrastructure parse if for me and make it available to my PHP code? Are ther开发者_StackOverflowe other, easier approaches to this?


Make it configurable through the admin panel, and set it to false by default. If it's in the code a user could toggle it anyway, so you may as well put it in the developer section of the settings panel.


Magento ECG gave me a good solution in the Magento forums: http://www.magentocommerce.com/boards/viewthread/226496/

Quoting them:

One of the solutions to set and get debug flag in your module is to add it to config.xml in your module’s etc directory.

You can add it to section . So your section will look like:

<default>
    <your_module>
        <debug>1</debug>     
    </your_module> 
</default>

And from the code you can get this:

$debugFlag = Mage::getStoreConfig('your_module/debug');

And what I'm going to do is put the same XML block in my local.xml file. That way it only kicks in on my development machine and does not get published with my Magento extension (as does the extension's own config.xml)


I would use the admin/system/config of Magento for this.

Add an appropriate etc/system.xml to your module, e.g.:

<config>
    <!-- : -->
    <tabs>
        <!-- : -->
        <mycompany>
            <label>My Company Tab</label>
            <sort_order>99</sort_order>
        </mycompany>
        <!-- : -->
    </tabs>
    <!-- : -->
    <sections>
        <!-- : -->
        <mymodule>
            <label>My Module</label>
            <tab>mycompany</tab>
            <frontend_type>text</frontend_type>
            <sort_order>99</sort_order>
            <show_in_default>1</show_in_default>
            <show_in_website>1</show_in_website>
            <show_in_store>1</show_in_store>
            <groups>
                <mygroup>
                    <label>My Group</label>
                    <frontend_type>text</frontend_type>
                    <sort_order>99</sort_order>
                    <show_in_default>1</show_in_default>
                    <show_in_website>1</show_in_website>
                    <show_in_store>1</show_in_store>
                    <fields>
                        <debug>
                            <label>Debug</label>
                            <frontend_type>select</frontend_type>
                            <source_model>adminhtml/system_config_source_yesno</source_model>
                            <sort_order>99</sort_order>
                            <show_in_default>1</show_in_default>
                            <show_in_website>1</show_in_website>
                            <show_in_store>1</show_in_store>
                        </debug>
                    </fields>
                </mygroup>
            </groups>
        </mymodule>
        <!-- : -->
    </sections>
</config>

If the section mymodule didn't already exist before, you also need to define access control for the module initially (must be put into etc/config.xml of your module):

</config>
    <!-- : -->
    <adminhtml>
        <!-- : -->
        <acl>
            <resources>
                <admin>
                    <children>
                        <system>
                            <children>
                                <config>
                                    <children>
                                        <mymodule >
                                            <title>My Module</title>
                                        </mymodule>
                                    </children>
                                </config>
                            </children>
                        </system>
                    </children>
                </admin>
            </resources>
        </acl>
        <!-- : -->
    </adminhtml>
    <!-- : -->
</config>

Now admins can change the defined debug flag thru the Magento backend by selecting

System -> Configuration - My Module - My Group - Debug - Yes|No

To get the current value of your debug flag programmatically you can use:

$sFlag = Mage::getStoreConfig('mymodule/mygroup/debug');     // null | '0' | '1'
$bFlag = Mage::getStoreConfigFlag('mymodule/mygroup/debug'); // true | false
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜