How to Add Magento 2 System Configuration Fields & Get Core Config Data Programmatically

- E-Commerce, Magento 2

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

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: 

  1. Add a new acl.xml file to the module’s etc folder;
  2. Define a role resource for the module’s system configuration. 

The article from Magento2 Blog contains the following code example:

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:

Magento 2 system configuration fields get core config data programmatically

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:

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:

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:

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:

If you add new injections without recompiling your module, The PHP Fatal error occurs. To solve it, compile the module using the following command:

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: Magento 2 system configuration fields and Magento 2 – get core config data programmatically.