views:

36

answers:

2

I created a custom Drupal module and now want to save some preferences for it. Those are essentially 3 arrays of strings that should be saved and also easily edited by a (administrative) user.

What would be the best way to do that in Drupal? I've read about variable_set and variable_get, are they appropriate to store module-specific data like this?

Is there some way to easily create an admin form to edit those variables, or do I have to write that from scratch?

+1  A: 

If you want to do it fast, use variable_get/set and system_settings_form().

Yorirou
Thanks, this looks exactly like what I need. But which form element(s) do I use for an array of strings? Can I just use one textfield and Drupal will figure out the rest (multiple textfields and serialize/deserialize the variable)?
Fabian
if you need multiple textfields, make something like that the cck does
Yorirou
A: 

Need more space than a comment. @yorirou has the correct answer. However, to store/retrieve an array of strings each entered by different widgets, you have an easy path:

Wrap everything in a fieldset with the #tree property. All fields within that fieldset will be folded into an array structured based on the fieldset name for the variable name, and the individual field names for the key value. Array serialization is automatically handled on variable_get/set.

$form['custom_settings'] = array(
  '#title' => t('My settings'),
  '#type' => 'fieldset',
  '#tree' => TRUE,
);
$form['custom_settings']['first_setting'] = array(
  '#type' => 'textfield',
  // etcetera
);
$form['custom_settings']['second_setting'] = array(
  '#type' => 'textfield',
  // etcetera
);

On output of the saved array, you would get something like this:

custom_settings = Array(
  'first_setting' => 'some string',
  'second_setting' => 'some other string',
)

You could get the same affect with a #validate handler, or using the #parents element in the forms arrays, but those would require extra work to wrangle the default value.

Grayside