Magento backend_model - 我需要为每个配置字段指定吗?

发布于 2024-11-04 02:46:23 字数 1228 浏览 1 评论 0原文

如果我想在保存自定义模块的特定配置字段时做一些额外的事情(除了保存到 Magento 核心配置表之外),我可以在我的 system.xml 中为该字段指定一个 backend_model ,并拥有该后端模型类扩展 Mage_Core_Model_Config_Data,覆盖 _afterSave,并将我的额外内容放入该方法中。

但是如果我想为多个领域执行此操作该怎么办?我不希望行为是保存 field1 并为该字段调用我的 afterSave,保存 field2 并为该字段调用我的 afterSave 等等。我宁愿将所有字段保存到 Magento 核心配置表中,并且然后我做我的额外的事情。

这可能吗?我想我也许可以使用事件/观察者来实现这一点。所以在我的 config.xml 中,部分,我添加了一个观察者,如下所示:

<events>
    <admin_system_config_changed_mysection>
        <observers>
            <mypfx_admin_system_config_changed_mysection>
                <class>mymodule/adminhtml_system_config_backend_configSaveObserver</class>
                <method>myConfigSaved</method
            </mypfx_admin_system_config_changed_mysection>
        </observers>
    </admin_system_config_changed_mysection>    
</events>

但是保存配置时不会调用我的观察者方法。也许我的活动名称有误?我猜测事件名称末尾的“mysection”位必须与 system.xml 中的部分相匹配:

<sections>    
    <mysection translate="label" module="mymodule">
        ...
        <groups>
            ...
        </groups>
    </mysection>
</sections>

谢谢。

If I want to do something extra when a particular configuration field for my custom module is saved (over and above saving to the Magento core config table), I can just specify a backend_model for that field in my system.xml, and have that backend model class extend Mage_Core_Model_Config_Data, override _afterSave, and put my extra stuff in that method.

But what if I have several fields I want to do this for. I don't want the behaviour to be to save field1 and call my afterSave for that field, save field2 and call my afterSave for that field, etc. I'd rather that all the fields were saved to the Magento core config table, and then I do my extra stuff.

Is that possible? I thought I might be able to achieve that using event/observer. So in my config.xml, <adminhtml> section, I added an observer as follows:

<events>
    <admin_system_config_changed_mysection>
        <observers>
            <mypfx_admin_system_config_changed_mysection>
                <class>mymodule/adminhtml_system_config_backend_configSaveObserver</class>
                <method>myConfigSaved</method
            </mypfx_admin_system_config_changed_mysection>
        </observers>
    </admin_system_config_changed_mysection>    
</events>

but my observer method is not called when the config is saved. Maybe I have the wrong event name? The "mysection" bit on the end of the event name I was guessing had to match the section from system.xml:

<sections>    
    <mysection translate="label" module="mymodule">
        ...
        <groups>
            ...
        </groups>
    </mysection>
</sections>

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

南笙 2024-11-11 02:46:23

您尝试侦听的事件不存在。以下是您想要做的事情,以及未来选择正确活动的一些提示。

首先,Magento 中的每个事件都是由 Mage::dispatchEvent 方法触发的。搜索这些调用的核心代码,您将始终知道要侦听的事件的名称。

$ ack 'admin_system_config_changed_'
Adminhtml/controllers/System/ConfigController.php
136:            Mage::dispatchEvent("admin_system_config_changed_section_{$section}",

从上面的内容中,您可以看到事件的名称与您认为的名称的对比。

admin_system_config_changed_section_{$section}
admin_system_config_changed_mysection

因此,您似乎在自己的部分名称之前缺少部分

其次,在开发盒子上工作时,找到您要查找的事件的最佳方法是在源头记录事件。暂时在dispatchEvent函数中添加一些调试代码。

#File: app/Mage.php
public static function dispatchEvent($name, array $data = array())
{   
    //either one of the lines below should do it.  One uses Magento's
    //built in logging, the other uses something more crude 
    #Mage::Log($name);
    #file_put_contents('/tmp/test.log',"$name\n",FILE_APPEND);    

    Varien_Profiler::start('DISPATCH EVENT:'.$name);
    $result = self::app()->dispatchEvent($name, $data);
    #$result = self::registry('events')->dispatch($name, $data);
    Varien_Profiler::stop('DISPATCH EVENT:'.$name);
    return $result;
}

这会将大量事件名称转储到您的日志中。我通常使用 OS X 的 Console.app 在请求期间查看日志文件,复制行,排序并删除重复项,然后最终得到这样的列表

admin_system_config_changed_section_commercebug
admin_user_load_after
admin_user_load_before
adminhtml_block_html_before
adminhtml_controller_action_predispatch_start
application_clean_cache
controller_action_layout_generate_blocks_after
controller_action_layout_generate_blocks_before
controller_action_layout_generate_xml_before
controller_action_layout_load_before
controller_action_layout_render_before
controller_action_layout_render_before_adminhtml_system_config_edit
controller_action_postdispatch
controller_action_postdispatch_adminhtml
controller_action_postdispatch_adminhtml_system_config_edit
controller_action_postdispatch_adminhtml_system_config_save
controller_action_predispatch
controller_action_predispatch_adminhtml
controller_action_predispatch_adminhtml_system_config_edit
controller_action_predispatch_adminhtml_system_config_save
controller_front_init_before
controller_front_init_routers
controller_front_send_response_after
controller_front_send_response_before
core_abstract_load_after
core_abstract_load_before
core_block_abstract_prepare_layout_after
core_block_abstract_prepare_layout_before
core_block_abstract_to_html_after
core_block_abstract_to_html_before
core_collection_abstract_load_after
core_collection_abstract_load_before
core_config_data_load_after
core_config_data_save_after
core_config_data_save_before
core_config_data_save_commit_after
core_layout_block_create_after
core_locale_set_locale
core_session_abstract_add_message
core_session_abstract_clear_messages
http_response_send_before
model_load_after
model_load_before
model_save_after
model_save_before
model_save_commit_after
resource_get_tablename
store_load_after
store_load_before

您仍然需要使用一些智能猜测以确定您想要哪个活动,但它们的命名足够直观,您通常可以找到您想要的内容。

The event you're trying to listen for doesn't exist. Here's what you want to do, and some tips for picking the right event in the future.

First, every event is fired in Magento by the Mage::dispatchEvent method. Search the core code for these calls and you'll always know the name of the event you want to listen for.

$ ack 'admin_system_config_changed_'
Adminhtml/controllers/System/ConfigController.php
136:            Mage::dispatchEvent("admin_system_config_changed_section_{$section}",

From the above, you can see the name of the event vs. what you thought it was

admin_system_config_changed_section_{$section}
admin_system_config_changed_mysection

So, it looks like you're missing the section before your own section name.

Second, while working on a development box, the best way to find the event you're looking for is to log things at the source. Temporarily add some debugging code to the dispatchEvent function.

#File: app/Mage.php
public static function dispatchEvent($name, array $data = array())
{   
    //either one of the lines below should do it.  One uses Magento's
    //built in logging, the other uses something more crude 
    #Mage::Log($name);
    #file_put_contents('/tmp/test.log',"$name\n",FILE_APPEND);    

    Varien_Profiler::start('DISPATCH EVENT:'.$name);
    $result = self::app()->dispatchEvent($name, $data);
    #$result = self::registry('events')->dispatch($name, $data);
    Varien_Profiler::stop('DISPATCH EVENT:'.$name);
    return $result;
}

This will dump a huge list of event names out to your log. I typically use OS X's Console.app to view the log file during the request, copy the lines out, sort and remove duplicates, and then end up with a list like this

admin_system_config_changed_section_commercebug
admin_user_load_after
admin_user_load_before
adminhtml_block_html_before
adminhtml_controller_action_predispatch_start
application_clean_cache
controller_action_layout_generate_blocks_after
controller_action_layout_generate_blocks_before
controller_action_layout_generate_xml_before
controller_action_layout_load_before
controller_action_layout_render_before
controller_action_layout_render_before_adminhtml_system_config_edit
controller_action_postdispatch
controller_action_postdispatch_adminhtml
controller_action_postdispatch_adminhtml_system_config_edit
controller_action_postdispatch_adminhtml_system_config_save
controller_action_predispatch
controller_action_predispatch_adminhtml
controller_action_predispatch_adminhtml_system_config_edit
controller_action_predispatch_adminhtml_system_config_save
controller_front_init_before
controller_front_init_routers
controller_front_send_response_after
controller_front_send_response_before
core_abstract_load_after
core_abstract_load_before
core_block_abstract_prepare_layout_after
core_block_abstract_prepare_layout_before
core_block_abstract_to_html_after
core_block_abstract_to_html_before
core_collection_abstract_load_after
core_collection_abstract_load_before
core_config_data_load_after
core_config_data_save_after
core_config_data_save_before
core_config_data_save_commit_after
core_layout_block_create_after
core_locale_set_locale
core_session_abstract_add_message
core_session_abstract_clear_messages
http_response_send_before
model_load_after
model_load_before
model_save_after
model_save_before
model_save_commit_after
resource_get_tablename
store_load_after
store_load_before

You still need to use some intelligence guessing to figure out which event you want, but they're named intuitively enough that you can usually find what you're looking for.

阿楠 2024-11-11 02:46:23

您需要将观察者方法绑定到特定的 Magento 事件(您可以添加自己的事件,但需要找到何时触发它并添加您自己的dispatchEvent 调用)。如果 Magento 有内置事件,请在配置中使用该事件名称。

网络上有一个内置事件列表的 pdf 文件 - google for it &你会找到的。

You need to tie your observer method to a specific Magento event (you can add your own, but need to find when you want it to be fired and add your own dispatchEvent call). If Magento has an event built in, use that event name in the config.

There's a pdf of the built in event lists on the web - google for it & you'll find it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文