How to add Magento 2 system configuration fields
To add custom Magento 2 system configuration fields to the backend, enabling store administrators to adjust specific module settings, you’ll need to use a system.xml file in your module’s etc/adminhtml directory. This setup is somewhat similar to Magento 1, but there are key differences.
When This is Useful
Adding custom system configuration fields is especially relevant if:
- You are developing a Magento 2 module
- You want to allow administrators to configure module-specific options in the backend
These configuration values are stored in the core_config_data table, similar to Magento 1.
Example: FTP Connection Fields
To illustrate, consider adding fields that allow administrators to input FTP details. You might create fields for:
- Server: a domain or IP address for the FTP server
- User: the FTP username
- Password: the password, stored in a secure format
- Path: the directory path for file import
These fields are necessary to enable the module to retrieve files from an external FTP server.
Step 1: Access Control List (ACL)
To control access, create an acl.xml file in the etc folder of your module and add a role resource for your module’s system configuration. Here’s an example structure:
Step 2: Define System Configuration Fields in system.xml
In the system.xml file, create a new tab, section, and group for your fields. Here’s an example structure:
custom_tab
Abbas_Acl::config
Key Attributes
Each field requires a label and several attributes:
- id: a unique identifier to access this field’s value
- type: field type (
textfor standard input,passwordfor secure entries) - showInDefault, showInWebsite, showInStore: determines if the field appears in the specified configuration scope
By setting up system configuration fields in this way, you enable easy configuration management for administrators and modular control over your Magento 2 setup.
Mohamed Abbas
Technical Lead | Magento Architect