We can consider an event as a kind of flag that rises when a specific situation happens for example when the user presses the « pay » button of your website, it is an event. Your order has been registered? Here it is an event too. You can read more about Events in Magento here.
An Observer is also called « Listener » he will listen to your program to detect events. When there is an event taking place, Observer in Magento will perform an action.
2. Create an Observer
An Observer in magento is in the directory Model of your plugin and extends the class Varien_Event_Observer (class which I think is clear …), go in /app/code/local/Pfay/Test/Model/ folder and create your Observer.php
- class Pfay_Test_Model_Observer extends Varien_Event_Observer
- public function __construct()
- public function saveCmsPageObserve($observer)
- $event = $observer->getEvent();
- $model = $event->getPage();
Here we’ve created an Observer with a function saveCmsPageObserve , this function is the method that will be executed when a page CMS will be saved in the backend of Magento.
In our example we stops the program to display a message but instead of a true die(), you will create a process to record additional information on your page in another table (for exemple)
3. Ensures that the Observer is declared in Magento
Now we have created our Observer , we need to declare it in Magento, how it’s done?
We will edit the config.xml our plugin. So open the file:/app/code/local/Pfay/Test/etc/config.xml
And in , after or after add:
- <method> saveCmsPageObserve</method>
- </ Pfay_Exemple_Model_Observer>
Ready to foster your knowledge with MAGENTO 2 BASIC GUIDE