Page 5 - Magento 2 Development
Develop new features, perform task programmatically and hon your developer skills
- 2 min read
Have you ever wondered how easy it is to check the quality of your own code, your colleagues' code, or a third-party module you want to use on a Magento project?
The Magento development team has created the Magento Extension Quality Program Coding Standard (Magento EQP), which allows you to check the code compliance with the standard, as well as identify the following shortcomings:
- SQL queries execution in the loop;- use of dangerous functions;- use of super-global variables;- excessive code complexity;- Unjustified collections loads.
Installation
You can install the Magento EQP via Composer, running this CLI command:
git clone https://github.com/magento/magento-coding-standard.gitcd magento-coding-standardcomposer install
In case you don't have the composer, you can download it here.
Checking
To check the code (which is, for example, in the /path/to/your/extension folder), go to the Magento EQP folder:
cd /path/to/magento-coding-standard
and run the followingihor
- 3 min read
Imagine you are cooking pasta and sit down to play some game. You listen with one ear to see if the "dish" does not boil away. In this case, you are an observer. When the pasta starts to boil away — the event "pasta_began_to_boil_away" is triggered, which causes you (the observer) to rush to the kitchen (perform an action, an algorithm).
In Magento 2, as in real life, there are observers and events that are implemented on the basis of the "Publish-subscribe pattern". We have already described , which allows you to extend and change the functionality of the store. Let's see how you can do this with the help of events and observers in Magento 2.
Events
Events are triggered by Magento 2 modules when an action has occurred or has to occur. Magento has many native events and also allows you to call your own event.
Use the dispatch method of the \Magento\Framework\Event\ Manager class to call an event.
For example:
namespace MyCompany\MyModule;class MyClass{
/** *ihor
- 2 min read
You often need to change the JavaScript code logic located in the .js file. The easiest way is to override the js file is by using a theme. You can learn how to override view files in the article about . This is a quick but not an elegant way.
To change one or more js-file methods, use the mixins available in RequireJS.
To extend this file:
app/code/VendorName/ModuleName/view/%area%/web/js/folder1/folder2/somefile.js
with the following code:
define( [ 'jquery', 'underscore', 'ko', 'uiComponent', 'uiRegistry', ], function ( $, _, ko, Component, registry, ) { 'use strict';
return Component.extend({ // ... method1: function() { /* some code */ }, method2: function() { /* some code */ } // ... });
});
%area% - the area where the file is extended, for example: frontend, adminhtml, base.
To override theihor
- 1 min read
All PHP programmers are familiar with the superglobal variables such as $_GET, $_REQUEST, $_POST, but their direct use is not allowed according to Magento2 code standards. So don't use them in your Magento 2 projects, especially if you want to pass the code Technical Review on the Magento Marketplace.
So what methods should be used?
To get data from the query, use the following methods: getParam($key, $default = null);getParams();getPostParam($key, $default = null);getPost();of the \Magento\Framework\App\RequestInterface class, for example:
protected $request;public function __construct( \Magento\Framework\App\RequestInterface $request, ....//інші параметри вашого класу) { $this->request = $request; ...//інший код конструктора}public function example() { // $data = $_REQUEST; $data = $this->request->getParams(); // $id = isset($_REQUEST['id']) ? $_REQUEST['id'] : null; $id = $this->request->getParam('id'); // $name = isset($_REQUEST['name'])ihor
- 1 min read
In Magento 2, one of the new properties is Magento 2 Virtual Type. What are they for?
Imagine that you already have a class A available:
namespace VendorName\ModuleName\Model
class A{ protected $arg1; protected $arg2; protected $arg3; public function __construct( Argument1 $arg1, Argument2 $arg2, Argument3 $arg3 ) { $this->arg1 = $arg1; $this->arg2 = $arg2; $this->arg3 = $arg3; }
}
And you need to create a class B, which will be inherited from A. However, it must take another argument as $arg2. To do this, you will create a new PHP file for class B:
namespace Me\MyModule\Model
class B extends A{ public function __construct( Argument1 $arg1, SomeOtherArg $someOtherArg, Argument2 $arg3 ) { parent::__construct($arg1, $someOtherArg, $arg3) }
}
But you don't need to do this in Magento 2. In such cases, virtual types are created. Therefore, create the etc/di.xmlihor
- 1 min read
Use the following code to insert the template block in WYSIWYG (Important! add it in inside double curly braces):
block class="Magento\Framework\View\Element\Template" template="Magefan_Blog::content/block-template-example.phtml"
The template identifier VendorName_ModuleName::some-template.phtml should be changed.
Attention! You can not use the block tag in the WYSIWYG product and category attributes. To make changes, install the magefan/module-catalog module.
- 2 min read
Before you install Magento 2, make sure that the webserver (such as ) and are configured and meet Magento 2 requirements.
Install the virtualhost script for the LAMP server, and create a new virtual host dev.mymagento.com, run the command:
virtualhost create dev.mymagento.com
Open this link in your browser and make sure you can see the content, not the error.
Download the archive with the latest version of Мagento 2. When downloading, you can select the "Include sample data" option, so that after you install Magento 2 test products and categories are available in the store.
Once you download the archive, place the folders and files from it in your domain directory /var/www/devmymagentocom.
Edit the virtual host domain configuration file, run the command:
gedit /etc/apache2/sites-available/dev.mymagento.com.conf
and change the string
DocumentRoot /var/www/devmymagentocom
to
DocumentRoot /var/www/devmymagentocom/pub
The pub folder must be the root one for the webserver.
After saving theihor