2

HALP! This is my first attempt at this kind of thing. It seems that the function won't fire when the event happens. (Eventually, what's supposed to happen is that when the event

Mage::dispatchEvent('nextbits_form_save_after',array('form_data'=>$params, 'form_model'=>$formModel));

occurs, the form data will be routed to appropriate people based on the data contained in the form.....)

I haven't written that code yet, but I don't see the observer catching the event and doing anything at all. What is the problem with my event/observer structures?

Observer.php: [\app\code\local\Westcott\EmailRouter\Model\Observer.php]

<?php

class Westcott_EmailRouter_Model_Observer {

    public function Notify_by_Mail($observer){

        if (isset($observer['form_data'])) {
            $form_data = $observer['form_data'];
            //$form_model = $observer['form_model'];

            foreach ($form_data as $key =>$fd) {
                //Do something with the form data
                echo "K: ".$key." => ".$fd."<br/>";
            }
            /*
            foreach ($form_model as $key =>$fm) {
                //Do something with the form data
                echo "K: ".$key." => ".$fm."<br/>";
            }
            */
        } else {
            //Do something else with the form data...
        }

    }

    echo "Hello!!";
}

Westcott_EmailRouter.xml: [\app\etc\modules\Westcott_EmailRouter.xml]

<?xml version="1.0"?>
<config>
  <modules>
    <Westcott_EmailRouter>
        <active>true</active>
        <codePool>local</codePool>
    </Westcott_EmailRouter>
  </modules>
</config>

config.xml: [\app\code\local\Westcott\EmailRouter\etc\config.xml]

<?xml version="1.0"?>
    <config>
        <modules>
            <Westcott_EmailRouter>
                <version>0.1.0</version>
            </Westcott_EmailRouter>
        </modules>
        <global>
            <models>
                <emailrouter>
                    <class>Westcott_EmailRouter_Model</class>
                </emailrouter>
            </models>
            <events>
                <nextbits_form_save_after>
                    <observers>
                        <Westcott_EmailRouter>
                            <!--<type>singleton</type>-->
                            <class>westcott_emailrouter/observer</class>
                            <method>Notify_by_Mail</method>
                        </Westcott_EmailRouter>
                    </observers>
                </nextbits_form_save_after>
            </events>   
        </global>
    </config>
gnicko
  • 400
  • 3
  • 20

2 Answers2

3

Hello There are issue in events tags... model class declaretion also Notify_by_Mail is not exiting in observer.php

....
<observers>
                        <westcott_emailRouter>

                            <class>emailrouter/observer</class>
                            <method>doNotifyBySubmittedContent</method>
                        </westcott_emailRouter>
                    </observers>
...
Amit Bera
  • 77,456
  • 20
  • 123
  • 237
  • I wanted to answer, when I saw this. In other words: You named your model group name as emailrouter, but you declare your observer class alias with westcott_emailrouter/observer. The latter should be emailrouter/observer. – nevvermind Apr 09 '14 at 20:37
  • i cannot understand ,what u asked – Amit Bera Apr 10 '14 at 13:54
1

First make sure the event your observing exits:

grep -rin -B2 -A2 "Mage::dispatchEvent" app/* > events.txt

View events.txt and search for nextbits_form_save_after

Second, as others mentioned your observer method isn't defined in your Observer.php

<method> should be doNotifyBySubmittedContent instead.

You can also get a list of events with a quick core hack:

You may also want to verify that the eventDispatch you're trying to observer is in fact being called during the stack as well.

* EDIT *


$observer will not have ['form_data'] you'll need to get the Event Request.

$request = $observer->getEvent()->getRequest()->getParams();
if(is_set($request['form_data'])) {

* EDIT #2 *


Just realized I had getParams() as the dispatched Event is magic getter/setter NOT a URI request.

Something like this should work if you're sure the event is being dispatched and form data is present:

$event = $observer->getEvent(); $formData = $event->getFormData(); // magic get/set $formModel = $event->getFormModel(); // magic get/set zend_debug::dump($formData); zend_debug::dump($formModel);

B00MER
  • 8,307
  • 2
  • 21
  • 49
  • 1
    OK... Thanks, Boomer. I assumed the event was in there but it wasn't for some reason. I purchased the extension and was told by the vendor's support staff that it was there....I'll have to take that up with them now, I guess. Also, I know about the observer method. (last minute edit while posting, etc.) Is there another problem with the class declaration? – gnicko Apr 09 '14 at 20:48
  • Quick additional note, zend_debug::dump(get_class_methods($observer->getEvent())); Will give you a list of available methods the observed event has. – B00MER Apr 09 '14 at 21:36
  • Thanks, @B00MER, I'm not sure what to do with that. I get "syntax errors near unexpected token `get_class_methods'".... I found a comment you posted elsewhere: "The approach I usually take is, a quick edit/save/revert to app/code/core/Mage/Core/Model/App.php and mage::log($eventName,null,'events.txt',true); to the dispatchEvent method. Load the page I'm trying to observe. Obviously don't leave this as is and revert once your var/logs/events.txt is created. Dirty, yes. Quick, yes. :) – B00MER Apr 15 '13 at 20:39" Do I read this correctly as temporarily adding the mage::log() to App.php? – gnicko Apr 10 '14 at 13:19
  • Yes, its a temp code meant to grab and dash of the events on the page you're attempting to hook into. Try $observer->getEvent()->getRequest() instead if you're getting an exception. – B00MER Apr 10 '14 at 16:32
  • I get no error, but I'm not getting any response from $observer->getEvent()->getRequest() either.... – gnicko Apr 10 '14 at 18:56
  • Updated my answer. Reference: https://www.demacmedia.com/magento-commerce/mini-tutorial-dispatching-your-own-event-in-magento/ – B00MER Apr 10 '14 at 20:53
  • Thanks again, @B00MER. I just tried the latest suggestion and I don't seem to get any kind of response back from that either. I tried several "versions" of the code above ($event = $observer->getEvent();....zend_debug::dump($formData);) and nothing "comes out". I tried it just as written (looking for screen output) and setting the debug statements to a variable and sending via email, etc. I am sure that the event is being dispatched and that my observer is firing... – gnicko Apr 11 '14 at 13:27
  • 1
    Not sure unfortunatly, perhaps a good reference may help: http://inchoo.net/ecommerce/magento/magento-event-driven-programming-tips-tricks/ some other events you may be able to hook into. – B00MER Apr 11 '14 at 23:09
  • 1
    $event = $observer->getEvent();

    $formData = $event->getFormData(); // magic get/set

    $formModel = $event->getFormModel(); // magic get/set zend_debug::dump($formData); zend_debug::dump($formModel);

    Was what I needed. Thank you, @B00MER

    – gnicko Apr 16 '14 at 14:45