2

I have to implement some functionality that requires different observers to "observe" different events. No problem here.
All the actions performed by these observers are basically the same, they just depend on a key (string). Let's call it 'something'.
What I've done so far, is to create a "parent" observer (abstract) and have child classes that extend the abstract observer and just implement a method:

public function getSomething(){
    return 'some_value';
}

In the abstract observer I have a method:

public function doSomeAction($observer){
   $something = $this->getSomething();
   //do magic with $something.
}

What I want to achieve, for extensibility reasons is to be able to pass to the event through config.xml this key so I won't need a new observer class and just be able to call the abstract observer (that is not going to be abstract anymore) with that parameter.
Something like:

<some_event_name>
    <observers>
        <unique_observer_key>
            <type>singleton</type>
            <class>my/observer</class>
            <method>doSomeAction</method>
            <something>some_value</something><!-- need to pass this -->
        </unique_observer_key>
    <observers>
</some_event_name>

[Edit]
I found that you can pass some arguments in the event xml like this:

<some_event_name>
    <observers>
        <unique_observer_key>
            <type>singleton</type>
            <class>my/observer</class>
            <method>doSomeAction</method>
            <args>
                <something>some_value</something>
            </args>
        </unique_observer_key>
    <observers>
</some_event_name>

I deleted the question after finding this. But I undeleted it after further digging because those parameters are USELESS. They are never used or passed to the class instance that handles the event.
Any ideas on how should I tackle this?

2

After digging I found out that what I want is not possible without rewriting the App model.

2

Maybe I am oversimplifying this a bit but couldn't you just do

public function doSomethingWithValue($observer)
{
    $this->_doSomething($observer, 'some value');
}

public function doSomethingWithOtherValue($observer)
{
    $this->_doSomething($observer, 'some other value');
}

protected function _doSomething($observer, $arg)
{
    // your code
}

and then use doSomethingWithValue and doSomethingWithOtherValue in your config.xml?

  • +1 because this is a possible solution to my problem, but I cannot accept it because it's not an answer to my question :). Yeah, this is a valid approach, but I implemented it using different observer classes for different events mainly for consistency reasons. I wanted to separate the actions related to products from the ones related to customers for example. So I have 1 observer for each entity. I wanted to make everything inside an observer but without duplicating methods. Anyway you can see my code here: github.com/tzyganu/GridEnhancer in the observers folder. – Marius Dec 10 '13 at 7:50
  • I figured you had your reasons - will need to check out the extension. Thanks for sharing it. – Kristof at Fooman Dec 10 '13 at 23:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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