Magento backend_model - do I need to specify for each config field?

后端 未结 2 1412
鱼传尺愫
鱼传尺愫 2021-01-22 09:50

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

相关标签:
2条回答
  • 2021-01-22 10:31

    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.

    0 讨论(0)
  • 2021-01-22 10:35

    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.

    0 讨论(0)
提交回复
热议问题