I have a form that is a system_settings_form. The form is very large and there are a lot of settings. I was wondering if there is a way to create an array (as there is in php) as follows using this form:
function module_admin_form(){
//...A bunch of settings...
$first_names = variable_get('first_name');
foreach($array as $key){
$form[$key]['first_name[]'] = array(
'#type'=>'radios',
'#title'=>t($key.' First Name'),
'#options' => $options,
'#default_value'=> $first_names[$key],
'#description'=>t("Which is the first name for %s?.", array('%s'=>$key))
);
}
//...A bunch of settings...
return system_settings_form($form);
}
Basically, I want the first_name variable to be stored in the database as a an array. However, when you save the form, it only saves the last value that you created. I also tried to put a key in the first_name array, but it saved each value separate. I know that I could save this form myself, but I would prefer not to because there are so many settings on the form. Is there a way that I could just handle these values myself (the values inside the foreach loop) and let the settings form handle the rest? Or is there a way to get it to save this as an array?
Comments
Serialize the array
I didn't completely understand your code and what you want to achieve but you may try using php's serialize function. It basically converts your array (or any other value type) into a string. You can save that string in the database and then use its sister function unserialize to get back the array when you need it.
That isn't exactly what I
That isn't exactly what I want. The problem is that a system settings form does all the saving for you. I would like to do the saving of just this section of code myself and not everything. I was wondering if there is a way for me to handle just a few pieces of a form in my own submit handler, and let the standard submit function for system settings form do the rest.
Different options
I see. I guess then you can try these:
system_settings_form(drop the line involving$form['#base']) andsystem_settings_form_submit. Then you have the power to save your data however you want.system_settings_form_submit. This is the most dirty solution and I don't recommend it if you are not 100% sure what you are doing.I would go with the first option but it is your call.
K - I knew I could do that,
K - I knew I could do that, but I was seeing if there was another solution. Thanks a lot.
This is crazy old but figured
This is crazy old but figured I would post what I did.
I changed this from what I actually did so this is untested. But I think the idea is there for anyone else that needs a solution like this.
A cleaner way - use submit handler
A cleaner way to do this is to do it in a submit function rather than a validate function.
The trick is to make sure your custom submit function fires before system_settings_form_submit(). To do that, add it to your form with array_unshift() like so:
And then put the logic to serialize in the mymodule_submit(). I also simplified what you did to store the values before unseting the form_state key:
-----
BeFused - Drupal tutorials
Use #tree
IMO the cleanest way to do this is using #tree. The nested values will appear in $form_state as an array, and system_settings_form() will automatically save the variable as an array for you.
For example...
On right tracks but a bit
On right tracks but a bit wrong. as a Tree, fields needs to have parents as $form['my_array'] ['my_field_'. $i] = array(
Adriadrop Drupal development
+1 for #tree
#tree is the right answer.