I have added a basic admin form field in system.xml
as follows:
<field id="do_something" translate="label comment" type="checkbox" sortOrder="5" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Do something?</label>
<comment>Do we want to do something?</comment>
</field>
The field renders correctly, and saves with no errors, and a row is added to core_config_data, but the value is always NULL
regardless of whether the box was checked or not.
What am I missing?
Best Answer
As far as I know, by default Magento, we cannot save the value of checkbox with a simple declaration of checkbox type in xml config. So, we need to create our own custom renderer for this config.
Create admin config: app/code/Vendor/YourModule/etc/adminhtml/system.xml
Basically, our config declare a frontend model for rendering custom field.
Create frontend model block: app/code/Vendor/YourModule/Block/Adminhtml/System/Config/Checkbox.php
Config source value: app/code/Vendor/YourModule/Model/Config/Source/Checkbox.php
app/code/Vendor/YourModule/view/adminhtml/templates/system/config/checkbox.phtml
Alternative solutions
--In your case, We can use
type="select"
, and then try to add the Default Yesno Source.--Also use
type="multiselect"
if there are many values.