I have a custom observer in Magento 1.6.2.0 that is called when a CMS page is saved or deleted (events cms_page_delete_before/cms_page_save_before). I have verified (using Mage::log()) that the observer is working, however when I try the following:
public function getCmsUrl(Varien_Event_Observer $observer)
{
$url = $observer->getEvent()->getPage()->getIdentifier();
return $url;
}
I get nothing returned (rather than “about-us” or “enable-cookies” or whatever URL path the CMS page has). The following code, however, works perfectly fine:
public function getProductUrl(Varien_Event_Observer $observer)
{
$baseUrl = $observer->getEvent()->getProduct()->getBaseUrl();
return $baseUrl;
}
Can someone let me know what the correct way of accessing a CMS page is when passed via an observer?
Thanks in advance for any help/tips/pointers 🙂
The events
cms_page_delete_beforeandcms_page_save_beforeare fired inMage_Core_Model_Abstract. This it how it looks like in thebeforeSavefunction:As you can see, it uses a variable
_eventPrefixto construct the event key. In the CMS page model, this is set tocms_page.Also notice the part
$this->_getEventData(). This is how the model, in this case the CMS page, is passed to the observer:As you can see, the object has two names,
data_objectand a name defined in a variable,_eventObject. In the product model, the name is set toproduct, but in the CMS page model, the variable is missing. Apparently the Magento team forgot to put this in, and as a result, the default name from the core model is used:That means you can get the CMS page in your observer by using
getObject: