Custom setting for boolean checkbox is serialized as int value
When developing a custom extension and adding a new setting for a boolean checkbox in settings/myext.setting.php
file as:
<?php
return [
'myext_boolField' => [
'name' => 'myext_boolField',
'type' => 'Boolean',
'html_type' => 'checkbox',
'default' => 1,
'is_domain' => 1,
'is_contact' => 0,
'title' => 'Boolean checkbox setting',
'description' => '',
],
];
When this value is saved in Admin Form, the value is serialized as int i.e.: i:1;
. This invalidates the code that treat this setting as a boolean value like:
// ** This won't work
if (Civi::settings()->get('myext_boolField') === TRUE) {
// Do something...
}
. . .
// ** This will work
if (Civi::settings()->get('myext_boolField') === 1) {
// Do something...
}
We need to treat this value as integer in the code, which is not a programming problem, but I think it gets confusing about variable types.
In my investigation looks like the culprit is this casting to integer:
https://github.com/civicrm/civicrm-core/blob/master/CRM/Admin/Form/SettingTrait.php#L343
Edited by sluc23