It happens that you are frequently facing asynchronous customer data loading while working with Magento 2. For example cart reloading, checkout, logging and user log in and some others are working through AJAX. In order to work with these data, Magento development team provides with a proper interface, that we are going to overview now. (more…)
There are 2 main places for storing configuration values In Magento 2: database (the core_config_data table) and XML files. The configurations, stored in the database can be changed via the administrator panel, while the data, located in the XML files are of a technical nature and can be changed only by a developer.
It’s easy to use configuration files in Magento 2. Configuration files include:
- app/etc/config.php — contains the declaration of all modules;
- app/etc/env.php — describes the array, which contains the front end name for the back end panel, the data for connection to the database, the table prefixes, the current store mode, the types and statuses of the cache.
These files are generated during the Magento 2 setup. You can change them directly editing the file. However, running terminal commands bin/magento is considered to be best practice.
- etc/config.xml — contains default option values from Stores > Configuration in the admin panel menu. This menu can be configured at system.xml;
- di.xml — contains configurations for the dependency injection;
- etc/events.xml — a list of observers and events;
- etc/routes.xml — a list of routers;
- etc/config.xml — contains the default values for the module settings Stores > Configuration;
- etc/acl.xml — adds module resources to a resource tree that allows you to configure access for different users.
- etc/crontab.xml — adds and configures the task for the cronjob;
- etc/module.xml — announces the name and the version of the module, as well as its dependencies on other modules;
- etc/widget.xml — stores the widget settings;
- etc/indexer.xml — announces a new kind of indexing. It specifies the view_id parameter, which points at the views described in etc/mview.xml;
- etc/mview.xml — describes the representations of all the indices described in etc/indexer.xml;
- etc/webapi.xml — defines web API components, which service method to use and which resource to connect for a specific request;
- etc/view.xml — contains the properties of product images;
- etc/product_types.xml — describes types of products in a store;
- etc/product_options.xml — describes the types of options, that can have products and classes to render them;
- etc/extension_attributes.xml — a new ability to add a custom attribute appeared in Magento 2. This file describes the attribute, its type, which can be simple or complex and represent an interface;
- etc/catalog_attributes.xml — groups attributes;
- etc/adminhtml/system.xml — can only apply to the admin area, adds tabs to Stores > Configuration, describes sections and fields of a form;
- etc/adminhtml/menu.xml — can only apply to the admin area, adds an item to the admin panel menu.
Magento downloads different files and different areas separately. It has various loaders for each type of files.
There are 3 areas in Magento 2 XML configuration files: global, front end and admin. Some files with options may be different for each area, others may not. For example, event.xml can be for global, front end and admin for each area, while module.xml can only be global.
If the configuration file is located in the directory of an etc/module, its values are located in the global scope. To specify the scope of admin.html or front end configuration, you need to put the configuration file in the etc/frontend or etc/adminhtml folder respectively.
In Magento 1 the scope was defined by a separate branch in an XML file. In the new concept of Magento 2, this improvement allows you to load configurations separately for different scopes. If the same parameters are specified for the global and admin.html scopes, they will be merged and loaded together.
The loading of configurations occurs in three stages:
- Loading system-level configurations. Loading the files necessary to run Magento 2, such as config.php;
- Loading global area configurations. Loading the files located in the app/etc/Magento 2 directory, such as di.xml, as well as the files related to the global scope and located directly in the etc/module folders.
- Loading configurations for specific areas. Loading the files located in the folders etc/adminhtml or etc/frontend.
Configuration files are connected according to their complete xPaths. Specific attributes are defined in the $idAttributes array as identifiers. After 2 files are connected, they contain all the nodes and values from the original files. The second XML file either adds or replaces the nodes of the first XML file.
Each type of XML file is validated by the corresponding XSD validation scheme. All validation schemes are in the etc/directory of the module. The most important schemes are in the Magento framework (vendor/magento/framework), for example, XSD for acl.xml is located in the vendor/magento/framework/Acl/etc/acl.xsd directory.
All Magento 2 configuration files are processed by the Magento/Framework/Config component. This component loads, merges, validates and processes the configurations, converting them into an array of the necessary format. If you want to change the default boot procedure, you need to replace the classes of Magento/Framework/Config by implementing certain interfaces of this component.
Magento/Framework/Config provides the following interfaces for developers:
- \Magento\Framework\Config\DataInterface — returns a value by key within the scope, connects the configuration data to an object;
- \Magento\Framework\Config\ScopeInterface — identifies the current scope;
- \Magento\Framework\Config\FileResolverInterface — identifies a set of files that must be read;
- \Magento\Framework\Config\ReaderInterface — reads configuration data;
- \Magento\Framework\Config\ConverterInterface — converts a DOM object to an array;
- \Magento\Framework\Config\SchemaLocatorInterface — determines the path to validation schemes;
- \Magento\Framework\Config\ValidationStateInterface — determines the current validation status.
Class groups used to load XML:
- Config is used to access the configuration values;
- Reader is used to read the file;
- SchemaLocator stores the path to the validation scheme.
Magento 2 provides two types of validation for XML configuration files: validation before a merging and validation after a merging. It can be either the same or different schemes.
To create a custom configuration file, you need to create the following elements:
- XML file
- XSD schema
- Config PHP file
- Config reader
- Schema locator
Let’s take the product_types.xml module of Magento_Catalog as an example of a custom configuration file. Each module can add its own product type using the product_types.xml file and these files will be validated and merged.
- We will start with creating an XSD file. product_types.xsd validation scheme is used in Magento_Catalog before merging, product_types_merged.xsd is used for a merged XML file.
- Create a PHP configuration file to access the data from the file. In our example it is Config.php. To provide access to data from the product_types.xml file, it implements the Magento\Catalog\Model\ProductType\ConfigInterface interface and all its methods.
- In Config.php of the constructor, we need to get a reader class. In our case, it is Magento\Catalog\Model\ProductType\Config\Reader. This is a small class with the definition of the $_idAttributes property. In the constructor of the $fileName variable, we define the name of the XML file.
- Magento\Catalog\Model\ProductType\Config\SchemaLocator implements two methods: getSchema and getPerFileSchema and returns the path to merged XSD and ordinary XSD files. In the constructor, we define these paths in the properties of $_schema and $_perFileSchema.
- Creating a converter class. In our example: Magento\Catalog\Model\ProductType\Config\Converter implements \Magento\Framework\Config\ConverterInterface and realizes the convert method, which converts the node’s DOM tree to an array.
That is it concerning configuration XML, variables scope and configuration files in Magento 2. Feel free to ask your questions in the comment section below, if something is still unclear to you, and our developers will answer them shortly.