Magento – Creating an Observer with selectable event

event-observermagento-1.9module

I am looking for a way to list in a dropdown all events between when order is created and an order is finished (ie sales_order_shipment_save_after or sales_order_invoice_register) in the Admin panel. When whatever selected event is hit, then fire off whatever is inside my Observer (post to url). I am having an issue though as I understand you need to explicitly provide the event you want to trigger inside of your modules config.xml like so

<?xml version="1.0"?>
<config>
    <modules>
        <Namespace_Modulename>
            <version>0.1.0</version>
        </Namespace_Modulename>
    </modules>
    <frontend>
        <events>            
            <checkout_submit_all_after>
                <observers>
                    <Namespace_Modulename_Customevent>
                        <type>singleton</type>
                        <class>Namespace_Modulename_Model_Observer</class>
                        <method>customFunction</method>
                    </Namespace_Modulename_Customevent>
                </observers>
            </checkout_submit_all_after>
        </events>
    </frontend>    
</config>

If this is the case then how would I be able to create a more configurable Event/Observer that would be able to handle admin specified events?

Best Answer

You will likely have to attach the event observer to all the events, then check at the start of the observer if the triggered event is the one configured in the admin panel:

public function myEventObserver(Varien_Event_Observer $observer)
{
    $eventName = $observer->getEvent()->getName();
    $configuredEvent = Mage::getStoreConfig('your/path/here');
    if ($configuredEvent !== $eventName) {
        return;
    }
    // your observer code
}
Related Topic