Tutorials

Tutorials about HTML, CSS, PHP, Javascript, and Photoshop

  • Home
    Home This is where you can find all the blog posts throughout the site.
  • Categories
    Categories Displays a list of categories from this blog.
  • Tags
    Tags Displays a list of tags that has been used in the blog.
  • Archives
    Archives Contains a list of blog posts that were created previously.
  • Login

Understanding Magento Observers

by in Photoshop
  • Font size: Larger Smaller
  • Hits: 2919
  • 0 Comments
  • Subscribe to this entry
  • Print
2919

But before we dive into the specifics, let's make sure that we have a clear understanding of the design patterns that are in play in the Magento application, Magento has a pretty well implemented Event/Observer system

How it works and what it is, lets' talk about the concept of the event system, Specifically. Though we've covered certain design patterns in other articles on Tuts+, we haven't done so within the context of Magento.  

Let's take a look at the pattern in order to gain a clear understanding of how it is, how it works, and how it's beneficial, To that end

Design Patterns as a Whole

Over the years a wide array of design patterns have been dveloped. But whatIsA design pattern exactly

To quote Wikpedia:

A design pattern is a general reusable solution to a commonly occurring problem within a given context in software design, “In software engineering. ”

If we reduce that a little we're left with: a design pattern is a reusable solution to a common problem. That is as perfect a description.  

The design pattern is a template that can be used to create a solution that will fit problems of the same nature. The pattern itself is not the actual solution, it defines the way the solution can be implemented as a guideline.  

Your actual solution, even if adhering to the defined pattern can be different from mine as long as they both follow the guidelines set out by the design pattern

The Observer Pattern

Now that we know what a design pattern really is, lets take a look at the Observer Pattern

The Observer Pattern has turned out to be a very valuable addition to the architecture, First implemented in the MVC architecture. In part because of this an enormous amount of libraries have implemented the pattern over time and if you've been programming for a while it's not unlikely you've already used it

Let's take a look at how it works, Enough history now though

To observe something is to look at it, to observe its state, or. In a technical context, this is no different. Events, Observers by definition are watching something and in our case. The observers that are observing this event get executed, The idea here is that if an event gets dispatched.  

The subject that is being observed will be passed along by reference in order for the observer to be able to update or check it for changes, Generally

Check out this example put together from the Magento code:

Array('product' => $this));
        return $this;, array('product' => $this));
        $this->_getResource()->validate($this);
        Mage::dispatchEvent('catalog_product_validate_after', class Mage_Catalog_Model_Product
    extends Mage_Catalog_Model_Abstract
{
    public function validate()
    {
        Mage::dispatchEvent('catalog_product_validate_before'
    }
}

So this is the validate method as its available in your run of the mill Magento installation, Alright. For clarity I replaced two variables with their actual values. You can see two events are dispatched hereCatalog_product_validate_beforeAndCatalog_product_validate_afterEach of these gives the product object along with the dispatch so that the observer might do something with it

Class Enterprise_AdminGws_Model_Models
    extends Enterprise_AdminGws_Model_Observer_Abstract
{
    public function catalogProductValidateAfter(Varien_Event_Observer $observer)
    {
        if ($this->_role->getIsAll()) {
            return;
        }$product = $observer->getEvent()->getProduct();
        $this->_forceAssignToWebsite($product->getWebsiteIds());
    }
}

And here's an observer class and its method. This class and method are hooked into the system using Magento's configuration system. The event itself is handed to the observer class and the product is reachable from the event

I'll cover the actual creating and hooking of observers as well as reaching the subjects with more detail in the next post

Why Use This Pattern?

Or its close relatives like the Pub/Sub Pattern is the modularity it provides, One of the primary arguments to use the Observer Pattern

Because anything can hook into an event that is being dispatched, you never have a strong reference to it. This is exceptionally useful in a setting where you're always working with modules that may or may not provide services to a specific element that may or may not be available.  

To use Magento again, I can build a module that will observe product changes as well as a module I built that fires off its own custom events. But using a Pub/Sub or the Observer Pattern I can make that dependency optional and only apply whatever it is my current module to it when it's available, Now, in a regular system this module would be dependent on the earlier module

What's Next?

How you can start using it in your own modules and provide them in your own modules as well to hook into from different ones, in the next post I'll explain how it's used in Magento, or for others to hook into, After this quick refresher on what exactly design patterns are as well as the more in depth look at the Observer Pattern

Read more: Understanding Magento Observers

0
Trackback URL for this blog entry.

Comments

  • No comments made yet. Be the first to submit a comment

Leave your comment

Guest Saturday, 21 October 2017

Testimonial

Thank you so much! We are very happy with our new website. It is easy to use and all of our customers tell us, they love it.

Contact Us

  • 13245 Atlantic Blvd. #4352
    Jacksonville, FL 32225
  • 904-240-5823