Magento 2 Custom System Configuration

Sep 4, 2018170Gennadiy Haritonov
Magento 2 Custom System Configuration

Magento 2 allows you to extend the system configuration using modules. This will allow you to integrate the configuration of your module into the Magento 2 system menu.

Adding such custom settings is done using the app/code/Vendor/Module/etc/adminhtml/system.xml file

And now in parts:

This xml code allows you to add a new tab to the settings menu of Magento 2.

Adding a new tab to the settings menu of Magento 2.

Next:

This code adds a new section and assigns it to the custom_tab tab. Here can be used any tab from existing ones.

The parameters showInDefault = “1”, showInWebsite = “1” and showInStore = “1” indicate the scope in which our section will be visible.

This code adds a new section and assigns it to the custom_tab tab. Any tab from existing ones can be used here.

The parameters showInDefault = “1”, showInWebsite = “1” and showInStore = “1” indicate the scope in which our section will be visible.

Elements

Simple Text Field

Yes/No Dropdown

Dropdown With The Custom Source model

And our custom model can look like this: app/code/Vendor/Module/Model/Config/Source/Custom.php

File Upload

<validate>validate-no-empty</validate> checks to ensure that the field is not empty when saving the configuration.

Depends tag allows you to show this field only when yesno_dropdown is set to “Yes”.

Textarea

Secret Field

type=”obscure” hides the field value from the frontend, but in this case, the data will still be stored as plain text.

Setting Magento\Config\Model\Config\Backend\Encrypted as a backend model allows you to encrypt the data in the database.

Most of the source models are located in app/code/Magento/Config/Model/Config/Source and backend models are located in app/code/Magento/Config/Model/Config/Backend.

Setting Default Value

To set the default value for any of our custom settings, you need to create the app/code/Vendor/Module/etc/config.xml file

Getting Values

We will use Helper Class to get the values from our configuration: app/code/Vendor/Module/Helper/Data.php

This method receives a string representing the path to the required option in the form of section/group/field. That means that to get the value of custom_text field, we need to pass the string custom_section/general/custom_text.



Post a new comment

top
BelVG Newsletter
Subscribe to our mailing list and get interesting stuff and updates to your email inbox.
Email *