How to Add Magento 2 System Configuration Fields & Get Core Config Data Programmatically
In the article below, we shed light on how to add Magento 2 system configuration fields and describe how to get core config data programmatically. Both chapters include materials from the Magento 2 Blog. You can find more tips in the Magento 2 Developer’s Cookbook.
Table of contents
How to add Magento 2 system configuration fields
Let’s find out how to add custom Magento 2 system configuration fields to the backend section of your module. The goal of this procedure is to enable store administrators to edit certain parts of your module. In Magento 1, you had to specify changes in etc/system.xml. As for Magento 2, you should utilize a system.xml file as well. However, there are some minor nuances discussed in this article.
This chapter is useful for people who:
- work on a Magento 2 module;
- need to add system configuration fields to the Magento 2 backend.
You can find system configuration fields in your database in the core_config_data table. All the necessary information is available in the same place where you can find it in Magento 1.
The example from Magento 2 Blog explains how to add four new system configuration fields in Magento 2. The author of the article explores an importer module providing a detailed description of how to enable a store administrator to specify FTP connection details.
You need to add the following four elements:
- server – a field where an administrator can type a domain or an IP address of a remote server;
- user – an area to provide a valid FTP username;
- password – a field where a password that belongs to the user should be specified;
- path – a path necessary for the module to find import files.
From the perspective of the module, these Magento 2 system configuration fields are necessary to empower it to import all files under the specified path from an external server.
Note that all fields are:
- Simple text field;
- Each field has one exception;
- The password field supports passwords that are not plain text readable.
acl.xml
ACL is an access control list. As you might have already guessed from its name, acl.xml is a file where you provide different users with a separate access level. If you don’t want someone to change your modules system configurations, follow the steps below:
- Add a new acl.xml file to the module’s etc folder;
- Define a role resource for the module’s system configuration.
The article from Magento2 Blog contains the following code example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Acl/etc/acl.xsd"> <acl> <resources> <resource id="Magento_Backend::admin"> <resource id="Magento_Backend::stores"> <resource id="Magento_Backend::stores_settings"> <resource id="Magento_Config::config"> <resource id="Example_Importer::config" title="Importer" sortOrder="50" /> </resource> </resource> </resource> </resource> </resources> </acl> </config> |
Let’s explore the code above. By adding this snippet to your acl.xml file, you create a resource for Example_Importer. Replace Example_Importer with your module’s name using the following scheme: Vendorname_Extensionname. The file’s title is Importer. You can find it under System -> User Roles.
Now, click on the rule to see the following tree element in the “Role Resources” tab:
system.xml
After specifying different access levels, you can add Magento 2 system configuration fields. Create a new system.xml file under etc/adminhtml in the module’s directory. Below, you can see a code example utilized to create the four fields mentioned above:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd"> <system> <tab id="mstage_tab" translate="label" sortOrder="1000"> <label>Example</label> </tab> <section id="example_section" translate="label" type="text" sortOrder="100" showInDefault="1" showInWebsite="1" showInStore="1"> <label>Importer</label> <tab>mstage_tab</tab> <resource>Mstage_Importer::config</resource> <group id="general" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1"> <label>General</label> <field id="ftp_server" translate="label" type="text" sortOrder="20" showInDefault="1"> <label>Server</label> </field> <field id="ftp_user" translate="label" type="text" sortOrder="20" showInDefault="1"> <label>User</label> </field> <field id="ftp_password" translate="label" type="password" sortOrder="20" showInDefault="1"> <label>Password</label> </field> <field id="ftp_dir" translate="label" type="text" sortOrder="20" showInDefault="1"> <label>Directory</label> </field> </group> </section> </system> </config> |
It is also recommended to create a tab for your vendor name. Put all your modules there.
As for our example, it includes a submenu called Importer which contains a single general group. Leverage the group to hold all relevant FTP fields.
Note that there is nothing complicated in the configuration of input fields. The only requirement is that they need a label. And a field tag has these attributes:
- id – the field ID necessary to get the value by code. Define a useful name like in the case of variables;
- type – the field type: “text” for simple text inputs and “password” for passwords;
- showInDefault – set to 1 to add the field to the global scope. You can see the setting as a small hint in the backend;
- showInWebsite – set to 1 to let an administrator change the field by a website;
- showInStore – set to 1 to enable an administrator to change the field by a store view.
That’s all you need to know to add Magento 2 system configuration fields with different access levels. Now, let’s see how to get core config data programmatically in Magento 2.
With our importer, you don’t need to add any custom fields, since it is already flexible enough to satisfy even the most demanding business requirements. Meet the Improved Import & Export Magento 2 extension – your number one time-saver when it comes to ALL possible data transfers between Magento 2 and ANY external systems. For further information, follow this link:
Get Improved Import & Export Magento 2 Extension
How to get core config data programmatically in Magento 2
Magento 2 lets you not only create custom configuration fields for your module but also get config data programmatically from other modules or the Magento core.
Like in the previous chapter, we have to to to the core_config_data table where all Magento 2 backend settings are stored. The system saves new input fields in your module’s system.xml, generating new lines every time you save the form. To use this data within your code, utilize ScopeConfigInterface, injecting the following class:
1 2 3 4 5 6 7 8 |
class Example { protected $_scopeConfig; public function __construct(\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig) { $this->_scopeConfig = $scopeConfig; } ... |
This enhancement lets you load configuration data using the $_scopeConfig object. It is only necessary to specify the relevant path and scope. Note that you can save different values for each store view as well as work with data from different websites or a global scope.
The Magento 2 Blog article offers the following example of how to get the necessary information:
1 2 |
$storeScope = \Magento\Store\Model\ScopeInterface::SCOPE_STORES; echo $this->scopeConfig->getValue("example_section/general/field_data", $storeScope); |
Where
- example_section represents the id of your section;
- general represents the id of your group;
- field_data is the id of your field defined in system.xml.
The article also provides the following example of possible scopes:
1 2 3 4 5 |
const SCOPE_STORES = 'stores'; const SCOPE_WEBSITES = 'websites'; const SCOPE_STORE = 'store'; const SCOPE_GROUP = 'group'; const SCOPE_WEBSITE = 'website'; |
If you add new injections without recompiling your module, The PHP Fatal error occurs. To solve it, compile the module using the following command:
1 |
php bin/magento setup:di:compile |
Conclusion
As you can see, there is nothing complicated in adding custom system configuration fields and getting core config data programmatically in Magento 2. The above examples illustrate the simplicity of both processes. For further information, follow these links: