Wrote My First Magento 2 Module

It has been exciting to take a peek at the Magento 2 code now and then, and to view the changelog as new things are being added or modified. I figured it was about time to get a copy installed and create a module to start to learn the differences.

I have a simple developer toolbar module that I wrote that adds the bar to the top of the page, both in the admin and on the front end, that allows you to toggle logging, template path hints, profiler, cache, flush cache if on, and mark all unread admin notifications as read (if there are any). The module uses no template files at all and is all self-contained, so no matter what your design is, it always works.

Calling Models/Blocks/Helpers
The first noticeable difference is the way you call models, helpers, blocks, etc. No more shortcut, xpath-y style of calling things. You need to use the full class name now. Here are a few examples:

<?php
// Magento 1.x
Mage::getModel('core/cache');
Mage::getModel('catalog/product_type_configurable');
Mage::helper('catalog');
 
// Magento 2
Mage::getModel('Mage_Core_Model_Cache');
Mage::getModel('Mage_Catalog_Model_Product_Type_Configurable');
Mage::helper('Mage_Catalog_Helper_Data');

No More app/etc/modules/Your_Module.xml
That’s right, there are no more separate xml files for each module in app/etc/modules. Instead, you simply add the xml tags into the module’s config.xml file. At it’s most simple form, that simply means adding the active and codePool tags into your config.xml:

<config>
    <modules>
        <Your_Module>
            <version>1.0.0</version>
            <active>true</active>
            <codePool>local</codePool>
        </Your_Module>
    </modules>
    ...
</config>

No More Declaration of Module’s Models/Blocks/Helpers
In Magento 1.x, if you wanted to have your own model files for instance, you needed to declare that in your config.xml with code like this:

<config>
    <global>
        <blocks>
            <my_module>
                <class>My_Module_Block</class>
            </my_module>
        </blocks>
        <models>
            <my_module>
                <class>My_Module_Model</class>
            </my_module>
        </models>
        <helpers>
            <my_module>
                <class>My_Module_Helper</class>
            </my_module>
        </helpers>
    </global>
</config>

There is no longer any need for this due to the using of full classnames when calling models for instance. This really simplifies things and helps remove some room for error when writing modules.

Many Events Have Changed
The other difference is that the event that I was observing was no longer an event. I was extremely surprised to find that there was a different event that provided a LOT more flexibility and a LOT more options for doing cool things with it. I initially was frustrated when I found out that my observer wasn’t firing, but was thrilled to find out that Magento only made a way better option. So, as you begin to experiment and port your modules over to Magento 2, be slow to get frustrated. A lot has changed, and so far from my experience, it has been for the better!

I will continue to post more as I experiment with Magento 2.

This entry was posted in Magento. Bookmark the permalink.

6 Responses to Wrote My First Magento 2 Module

  1. Vinai says:

    Hey Josh, nice post! Which event is gone and what is the new one? Can you give a little more meat please?? Thanks!

  2. KoKoC says:

    Hello,

    You can also use a static tests located in dev/tests/static to help identify some incompatible places between Magento versions.

  3. Mark Shust says:

    Thanks for the write-up, good stuff

  4. Pingback: Magento 2 - When it's coming? | Magesoft.at Blog

  5. Bev says:

    It’s amazing in favor of me to have a website,
    which is valuable in support of my knowledge. thanks admin

  6. Vikram Purohit says:

    Hello,

    In Magento Ver 2.0 modules are placed in the ‘community’ code pool instead of local code pool. Is it true ? And what If I place my module in local code pool ? Will it violate any standards ?

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>