How To Create Observers | Customer logout

Prerequisities

Again we need our own mudule. Assuming we have module with namespace Guides and module name Meta

Static and dynamic events

We can divide Magento events into static and dynamic.

Static are:

Mage::dispatchEvent('admin_session_user_login_failed', array('user_name' => $username, 'exception' => $e));

Mage::dispatchEvent('cms_page_prepare_save', array('page' => $model, 'request' => $this->getRequest()));

Mage::dispatchEvent('catalog_product_get_final_price', array('product' => $product, 'qty' => $qty));

Mage::dispatchEvent('catalog_product_flat_prepare_columns', array('columns' => $columnsObject));

Mage::dispatchEvent('catalog_prepare_price_select', $eventArgs);

Dynamic are:

Mage::dispatchEvent($this->_eventPrefix.'_load_before', $params);

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

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

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

Mage::dispatchEvent('controller_action_layout_render_before_'.$this->getFullActionName());

Let’s say we want to execute our code before we edit new page in Magento admin panel. When you check core files and the save method, you will see this code

Mage::dispatchEvent('cms_page_prepare_save', array('page' => $model, 'request' => $this->getRequest()));

What interest us is the name of the event: cms_page_prepare_save.

Create observer

Create file app/code/local/Guides/Meta/Model/Observer.php

we will put this code inside

<?php
echo "Im here";
class Guides_Meta_Model_Observer extends Varien_Event_Observer {
  public function __construct() {

  }

  public function saveCmsPageObserve( $observer ) { // We chose this method name.
    $event = $observer->getEvent();
    $model = $event->getPage();
    print_r( $model->getData() );
    die( 'test' );
  }
}

Register Model and Observer in module config

<config>
    <modules>
        <guides_meta> <!-- **Note the lowercase -->
            <version>1.0.0</version>
        </guides_meta>
    </modules>
    <global>
        <models>
            <guides_meta> <!-- **Note the lowercase -->
                <class>Guides_Meta_Model</class> <!-- We have to register our model first. -->
            </guides_meta>
        </models>
        <events>
            <cms_page_prepare_save> <!-- This is the event name -->
                <observers>
                    <guides_meta>
                        <type>Singleton</type>
                        <class>guides_meta/observer</class> <!-- Than we assign our class with method which will be executed when event happens **Note the lowecase letters.. -->
                        <method>saveCmsPageObserve</method> 
                    </guides_meta> <!-- **Note the lowercase -->
                </observers>
            </cms_page_prepare_save>
        </events>
    </global>
</config>

Now go to your admin panel.

where-to-edit-page

Click on some page, edit it a click save.

Than you should see something like this.

edit page how it looks like


Lukas
Lukas Stranovsky
I am the guy who made this. Sorry for typos.