I want to create a configurable select list in forms.To achieve this what I did is I have created a text area, in that I will enter values like:


So, in form select options I have to use these values in a dropdown list. How can I achieve that. I am getting this text area data in form like below.Here I am getting data, but not displaying in the options.

$entityoptions = variable_get('entitydropdown'); 

I am getting the values as it is, but am not getting how can I assign these values to #options. I did like below but its not displaying the values.

'#options' => $entityoptions,

Please help me to identify, where I am doing wrong. Thanks in advance.

  • You can already make select lists with Drupal, out of the box. May we ask why you're trying to make something Drupal already does? – Raf Aug 22 '16 at 13:42
  • But I need conifgurable values. So only I am saving values in one admin panel text area, SO that whenever I want I can change values, and dynamicaly those values should display in form select options. – Suraj Aug 22 '16 at 13:46
  • Kinda like what Webform does? drupal.org/project/webform – Raf Aug 22 '16 at 13:50
  • Yes like that only,but I dont want to use any modules.I have created a custom form, so in that I want to assign these dynamic values from Texarea. – Suraj Aug 22 '16 at 13:56
  • Can you help me that, how can I retrieve data from text area in an array. I used explode('|', $entityoptions ); It works but,its failing to identify end of the line and next line. – Suraj Aug 22 '16 at 14:00

As requested in the comments, in answer format:

  1. Use the explode() function on your string
  2. Loop through the results and split each entry
  3. Add the result to the options array you feed to the select box


$options = array();
$lines = explode(PHP_EOL, $entityoptions);
foreach($lines as $line) {
  $line = explode('|', $line);
  $options[$line[0]] = $line[1];

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.