Heute beschäftigen wir uns einmal mit dem kommenden Magento 2 und dabei mit insbesondere der Modul Programmierung. Ziel ist es für Magento 2 ein Modul zur herausgehobenen Darstellung von ausgewählten Produkten (Featured Produkte) im Magento 2 Shop (vgl. folgender Screenshot). Da es sich um ein technischeres Thema handelt ist der Beitrag diesmal auf Englisch gehalten.
First Magento 2 module created
During our monthly hackathon last Friday one project was to create a Magento 2 module from scratch to learn some Magento2 lessons.
To help you get started with Magento 2 we are sharing our learnings in this post and the module source code on github (Magento2 Featured Products on Github).
The Magento 2 Featured Products module
The module we created allows you to mark products as featured products using a product attribute. In addition, we provide backend settings to set the number of featured products and the width of the product images. For the frontend display the Magento 2 featured product module provides a block, that can then be used to show the featured products to your customers for example on the home page of your shop.
Lessons learned with Magento2
- Currently, when you want to activate a Magento2 module, you need to activate it manually by editing the file app/etc/config.php – hopefully a better solution will arrive soon.
- All dependencies are handled by a dependency injection container, so in your __construct method simply list all the classes which your class needs. They will be included automatically. If you need special classes, you can provide a di.xml file in your /etc folder of your module.
- All classes are now namespaced, so the class name is only the last part, e.g. „namespace Lemundo\FeaturedProducts\Block;“ and then „class ProductList …“. The full name of the class is then Lemundo\FeaturedProducts\Block\ProductList
- The manipulation of attributes (addAttribute, updateAttribute etc) is now done in the data upgrade scripts (they were only rarely used in Magento1) instead of the sql update scripts. Thus, our module has a data/ folder where we are providing our data-install-0.0.1.php script. To get the correct setup class to add catalog attributes, you need to use the above mentioned di.xml file to tell Magento to use the correct class as a dependency.
- Setup scripts are not run automatically in the browser anymore which is nice since it had some bad side-effects on heavy-load servers. Instead, simply run „php setup/index.php update“ on the command line in the document root of the shop.
- The layout files are now named by the handle, so each layout file is exactly responsible for a single handle.
- To include a CSS file for your module, you can use <head><css src=“Lemundo_FeaturedProducts::css/module.css“/></head> inside your layout XML.
- All module files (code, design, css files, images, templates, …) are now together in one folder which is really nice.
- You can use less instead of CSS which makes for more compact code usage
- All the XML files now have .xsd description files, so you can easily look up which methods you can use :)
- The system.xml file for backend configuration values is a bit more compact now since the xsd defines that things such as showInDefault, showInWebsite and showInStore are now attributes instead of separate XML nodes.
- Most of the concepts from Magento1 still apply – once you figure out some of the specialties of Magento 2, you feel at home (however, a much safer, unit-tested home with less dependencies between the modules and a better overall architecture).
Next steps – Unit Tests
Our next goals are to test how well unit tests are working in Magento 2. In Magento1 it was possible to create unit tests when you installed an additional module and due to the bad design of Magento 1 it was very hard (and time-consuming) to write proper tests. Magento2 strives to solve these issues and has tests already integrated in the core, so it will be fun to play around with these and implement some unit tests for our module at the next hackathon.