Create Eventlist in Magento

Events in magento are very cool. Mostly events make it possible to build very modular extensions for magento. In my first steps, I always searched for a full list of magento events. The problem with these lists were, they aren't complete and outdated. And you will never find dynamic events like this.

Mage::dispatchEvent($this->_eventPrefix.'_load_after', $this->_getEventData());

On that example, it's good to see, that the variable $_eventPrefix can be very important in your own extensions.

How to find a special Event?

In my example, I wanted to find an event for Lesti_Fpc. That should be the event, were I check if a page is in cache or not. I added the following line in app/Mage.php

public static function dispatchEvent($name, array $data = array())
{
    Mage::log($name . ' => ' . implode(', ', array_keys($data)), null, 'events.log'); /* <- add this line and DELETE it after testing */
    Varien_Profiler::start('DISPATCH EVENT:'.$name);
    $result = self::app()->dispatchEvent($name, $data);
    Varien_Profiler::stop('DISPATCH EVENT:'.$name);
    return $result;
}

Be sure, that Logging in Magento is enabled and run the action. In My case, I only visited the homepage of my shop. Please erase this line after the action. You should see a file events.log in /var/log directory. Here is just a little part this file.

2013-07-08T14:58:37+00:00 DEBUG (7): resource_get_tablename => resource, model_entity, table_name, table_suffix
2013-07-08T14:58:37+00:00 DEBUG (7): resource_get_tablename => resource, model_entity, table_name, table_suffix
2013-07-08T14:58:37+00:00 DEBUG (7): resource_get_tablename => resource, model_entity, table_name, table_suffix
2013-07-08T14:58:37+00:00 DEBUG (7): resource_get_tablename => resource, model_entity, table_name, table_suffix
2013-07-08T14:58:37+00:00 DEBUG (7): resource_get_tablename => resource, model_entity, table_name, table_suffix
2013-07-08T14:58:37+00:00 DEBUG (7): resource_get_tablename => resource, model_entity, table_name, table_suffix
2013-07-08T14:58:37+00:00 DEBUG (7): core_collection_abstract_load_before => collection
2013-07-08T14:58:37+00:00 DEBUG (7): core_collection_abstract_load_after => collection
2013-07-08T14:58:37+00:00 DEBUG (7): core_collection_abstract_load_before => collection
2013-07-08T14:58:37+00:00 DEBUG (7): core_collection_abstract_load_after => collection
2013-07-08T14:58:37+00:00 DEBUG (7): core_collection_abstract_load_before => collection
2013-07-08T14:58:37+00:00 DEBUG (7): core_collection_abstract_load_after => collection
2013-07-08T14:58:37+00:00 DEBUG (7): controller_front_init_before => front
2013-07-08T14:58:37+00:00 DEBUG (7): controller_front_init_routers => front
2013-07-08T14:58:37+00:00 DEBUG (7): resource_get_tablename => resource, model_entity, table_name, table_suffix
2013-07-08T14:58:37+00:00 DEBUG (7): resource_get_tablename => resource, model_entity, table_name, table_suffix
2013-07-08T14:58:37+00:00 DEBUG (7): model_load_after => object
2013-07-08T14:58:37+00:00 DEBUG (7): core_abstract_load_after => data_object, object
2013-07-08T14:58:37+00:00 DEBUG (7): resource_get_tablename => resource, model_entity, table_name, table_suffix
2013-07-08T14:58:37+00:00 DEBUG (7): core_locale_set_locale => locale
2013-07-08T14:58:37+00:00 DEBUG (7): resource_get_tablename => resource, model_entity, table_name, table_suffix
2013-07-08T14:58:37+00:00 DEBUG (7): controller_action_predispatch => controller_action
2013-07-08T14:58:37+00:00 DEBUG (7): customer_session_init => customer_session
2013-07-08T14:58:37+00:00 DEBUG (7): controller_action_predispatch_cms => controller_action
2013-07-08T14:58:37+00:00 DEBUG (7): controller_action_predispatch_cms_index_index => controller_action
2013-07-08T14:58:37+00:00 DEBUG (7): model_load_before => object, field, value
...

You can see every event, that was thrown in your action and every param of the event. Now can you listen to this event in your modul-path/etc/config.xml. Here a little part of app/code/community/Lesti/Fpc/etc/config.xml.

<?xml version="1.0"?>
<config>
    <!-- ... -->
    <global>
        <!-- ... -->
        <events>
            <!-- ... -->
            <controller_action_layout_generate_blocks_before>
                <observers>
                    <fpc_controller_action_layout_generate_blocks_before>
                        <class>fpc/observer</class>
                        <type>singleton</type>
                        <method>controllerActionLayoutGenerateBlocksBefore</method>
                    </fpc_controller_action_layout_generate_blocks_before>
                </observers>
            </controller_action_layout_generate_blocks_before>
            <!-- ... -->
       </events>
       <!-- ... -->
    </global>
    <!-- ... -->
</config>

Here is the part of app/code/community/Lesti/Fpc/Model/Observer.php. I just have to grab the objects of the event with the magic getter-function.

<?php
public function controllerActionLayoutGenerateBlocksBefore($observer)
{
    /* many funny things going on */
            $layout = $observer->getEvent()->getLayout();
    /* more funny things */
}

The first version of Lesti_Fpc had customer_session_init as anchor-event. Cause I needed the session for replacing the dynamic blocks. But I had to add custom-handles like customer_logged_in on my to the layout. And handles from other extensions never find a way into my layout. It's very uncommon, that other extenions should need there own handle just for dynamic blocks. To be very compatible to other extension, I did choose the controller_action_layout_generate_blocks_before as anchor of my extension. Now I have a complete layout of the current page. I just have to remove every blocks, that isn't dynamic and can generate my tiny layout.

Next Previous