1

I have extended Mage_Catalog_Model_Category_Attribute_Source_Mode (and it's Resource model) to add two new modes display_mode values for Categories. Now I want to validate the value of that attribute when saving a category. Specifically, I want to make sure that is_anchor is always set to "No" when either of my two new modes are selected.

The problem is that neither Mage_Eav_Model_Entity_Attribute_Backend_Abstract::beforeSave nor Mage_Eav_Model_Entity_Attribute_Backend_Abstract::validate dispatch any events that I can listen for.

Mage_Catalog_Model_Category also has no _beforeSave method.

How can I validate category attributes on save? Thanks!

1
  • what about the event catalog_category_save_before ? Commented Sep 11, 2014 at 2:31

1 Answer 1

1

Actually Mage_Catalog_Model_Category does possess the event _beforeSave(). In order to see that, you need to look on that class with wide eyes :)

Mage_Catalog_Model_Category directly does not define _beforeSave. However you can see that, this class inherits from another class Mage_Catalog_Model_Abstract and it does possess _beforeSave. It looks like this

 protected function _beforeSave()
{
    $this->unlockAttributes();
    return parent::_beforeSave();
}

You can see that, this class again call its parent class. That parent class is nothing but Mage_Core_Model_Abstract. There you can see that _beforeSave defined like this.

protected function _beforeSave()
{
    if (!$this->getId()) {
        $this->isObjectNew(true);
    }
    Mage::dispatchEvent('model_save_before', array('object'=>$this));
    Mage::dispatchEvent($this->_eventPrefix.'_save_before', $this->_getEventData());
    return $this;
}

This function dispatches two events. Second one is important here. You can see that, it uses an event prefix in that event. Now take a look on Mage_Catalog_Model_Category. You can see that, it actually defines an event prefix there.

protected $_eventPrefix     = 'catalog_category';

This means, whenever category section is trying to save, _beforeSave method generates a unique event with name catalog_category_save_before. I think , you can use that event to save any category attribute

Hope that helps

1
  • Because there is no explicit way for me to tie into the category validation, I had to do a Mage::throwException from the _before_save observer, but it did the job. Thanks!
    – Tyler V.
    Commented Sep 11, 2014 at 22:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.