By marc.bau on
i need help. the following code is only working partly and i don't understand why. the "test_display" is saved well in variable table and after a reload of the module page the field is filled with the DB data. all ok here...
But if it comes to the "testVar1", this value is saved in variable table, but the field will never populate with the value if i reload the modules page. what is wrong here?
function test_settings() {
$prefix='test';
$form[$prefix] = array(
'#type' => 'fieldset',
'#title' => t('Global settings'),
'#collapsible' => true,
'#collapsed' => false,
);
$form[$prefix][$prefix.'_display'] = array(
'#type' => 'checkbox',
'#title' => t('enable/disable the test output'),
'#default_value' => variable_get($prefix.'_display', NULL),
'#description' => t('enable/disable the test output'),
);
$properties = array(
$prefix.'_test1' => t('Test 1'),
$prefix.'_test2' => t('Test 2'),
$prefix.'_test3' => t('Test 3'),
);
// Build the form for detailed settings
foreach ($properties as $a => $b) {
$form[$a] = array(
'#type' => 'fieldset',
'#title' => t('Array of properties for '.$b.' link'),
'#collapsible' => true,
'#collapsed' => true,
'#tree' => true,
);
$form[$a]['testVar1'] = array(
'#type' => 'textfield',
'#title' => t('Suboutput Test 1'),
'#default_value' => variable_get('testVar1', NULL),
'#description' => t('Suboutput Test 1'),
);
}
return $form;
}
thank you for help.
Comments
Part of the problem is you have 3 instances of testVar1
Part of the problem is you have 3 instances of testVar1, while you use
$form[$a]['testVar1']the value is stored under 'testVar1' three times (ince each for test1, test2 and test3. You might want to use this insteadThis way each of the variables has a unique name.
must be something different...
i tryed this, but it is not working, too. if i look inside the database "variable" table i see 3 variables named:
this variables contains a serialized array. the serialized array contain the variable "testVar1". therefor this "testVar1" is a uniqe member in the array variables listed above.
I went and looked at the code and think something like this ...
I went and looked at the code and think something like this will work. The issue lies with the fact the processing code only looks one level deep in the forms array and uses what ever it finds as the value. So I woul set '#tree' to false and change the element name in the loop, something like this
This way the processing code sees a "flat" form while the user still sees the fields sets and each variable has a unique name.
The processing code by the way is in system.module in the function system_settings_form_submit()
The '#tree' is *the*
The '#tree' is *the* problem. i'm not able to change this... ist must be saved with tree => true... if not i will start to create 100 variables, what is toooo slow. if you do a get_variable i will have 100 SQL requests... NO way!
with tree you will have only one serialized variable string in one variable... and this is how the save process is working. the problem is only the get_variable function that do not unserialize the string.
Maybe some else stumbles upon this post
Here's a solution I found for accessing varibales when #tree is set to true:
http://api.drupal.org/api/drupal/includes--bootstrap.inc/function/variab...