changeling
changeling

Reputation: 803

Magento: Observer not getting form

Magento 1.6.1 I am trying to get form data on my observer. I tried $observer->getEvent()->getForm();
and it's empty. here is some of my code

            <catalog_product_new_action>
            <observers>
                <namespace_module_save_product_stuff>
                    <type>singleton</type>
                    <class>Namespace_Module_Model_Observer</class>
                    <method>saveProductStuff</method>
                </namespace_module_save_product_stuff>
            </observers>
        </catalog_product_new_action>  

The event observer fires, but i can't get the form at all it just returns null.

public function saveProductStuff(Varien_Event_Observer $observer) 
    {
        $roleId = implode('', Mage::getSingleton('admin/session')->getUser()->getRoles());
        $roleName = Mage::getModel('admin/roles')->load($roleId)->getRoleName();
        if($roleName != 'Administrators'){

                try 
                {   

                    $form = $observer->getEvent()->getForm();
                    var_dump($form);
                }   catch (Exception $e)
                          {
                            Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
                          }

        }
    }

I really need to get this form on the observer but nothing is happening and ive spent looking at tutorials but all of them give you this method of doing things.

The event fires and i can do things inside it but not getting the form.

I need to get the form, then set a an element to disabled if its a certain type of user, i cant set it to locked because then the attribute won't save. I wanted to try something like this:

    $form = $observer->getEvent()->getForm();//returns NULL
$element = $form->getElement('attribute_code');
$value = 'test';

$element->setValue($test);
$element->setDisabled(true);

But no luck. I would really appreciate any help.

Upvotes: 0

Views: 2070

Answers (2)

changeling
changeling

Reputation: 803

I decided to lock the attribute

$product->lockAttribute('attribute_code');  

on the new action. Then added a second observer to prepare save, to unlock and save the attribute.

$product = $observer->getEvent()->getProduct();
$product->unlockAttribute('attribute_code');  

This did the trick.

Upvotes: 1

Joseph at SwiftOtter
Joseph at SwiftOtter

Reputation: 4321

Unfortunately, with this event, there isn't a way to get the form. Here is how it is called (app/code/core/Mage/Adminhtml/controllers/ProductController.php:192):

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

Depending on what you are trying to do, you may have to override some files. Please don't modify the core files! The reality is that if you are saving something, that isn't the time to be disabling a control. You will want to do that when the page loads.

Upvotes: 2

Related Questions