I would like to create a settings page for a user which allows them to save the options of a list which is to be used in a custom form. The list is called marketing source and it is added to a form which gets submitted with plain text values to an external API.

I know in field config for select lists you can do this but I want to do it on a custom config page for that specific list.

Foe example I would have a textfield in the config that had values like this


Then I need to convert that into a seralised array and store with variable set.

Is there a function that does that already for the field form?

Cheers Dan


This is how I achieved what I wanted, rather than use key value pairs I just used an empty textfield and used one line for each option and then when retrieving the textfield for the form I used the following.

$raw_marketing_options = explode(PHP_EOL, variable_get('marketing_sources_sources'));

I then just used the variable directly in the form.

$form['Marketing_source__c'] = array(
  '#type' => 'select',
  '#title' => 'Marketing source',
  '#options' => $raw_marketing_options,

There may be a safer way to do it as I am not sure how secure variable_set() is when storing plain text but only the administrator will see the textarea anyway.



You can use a system_settings_form this is a 'shortcut' creates a form that saves input data via a variable_set(). The tutorial also explains how to create a page for the form.

Tutorial on settings form

variable_set/variable_get always serialize/deserialize any array you pass to them.

  • You haven't really answered the question about saving an array for a select list, I already know how to create the form. – Daniel Harper May 12 '15 at 14:31
  • Oh now I get it, – Suranga Panagamuwa Gamage May 12 '15 at 14:32
  • Oh now I get it. I missread the question. Yes splitting on newline breaks is how I would do it to. If I were you I would not split the string every time but save the split version as a variable. Should save on some execution time. After exploding you can just sanitize every iteration of the array to be safe. (or even explode every part again but with the '|' item and build an associative array). – Suranga Panagamuwa Gamage May 12 '15 at 14:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.