I wanted to add values to a select list dynamically. Another module generates an array from a web service and I want to use those values to populate a select list in a form using hook_form_alter.

I tried directly adding the options in #options of the field using the following code

<?php $form['field_mage_category']['und']['#options']=$arr; ?>

The above code adds the options to the select list and returns a value according to the options selected but when the form is loaded again for editing. The option is set to default value and other issue is that when a single option is deleted. The respective value is not getting deleted from the field_data table.

I tried using #after-build property to populate the select list.below code is the call to _add_category_select_list function

      <?php function main_menu_form_alter(&$form, &$form_state, $form_id){
           form['field_mage_category']['#after_build'] = array('_add_category_select_list');
          } ?>

and following is the code in the _add_category_select_list function

<?php function _add_category_select_list($element, &$form_state) {
$mlib=new Mlibrary();
   foreach($main_category as $main){
       $element['und']['#options'] = array($main['category_id'] => $main['Title'])+  $element['und']['#options'];

return ($element);
   } ?>

The above code populates the select list but when the option is selected it is not returning any values for the options selected. Can anyone give me a proper way to add #options to select list dynamically using hook_form_alter or any other options that will allow me to add options from my module.

2 Answers 2


Your first method was very close; I wouldn't advise using an #after_build function for this simply because it's not necessary.

When you're dealing with field widgets added to forms the actual form elements (textfield, select, checkbox, etc.) are a little further down in the array (see Why is hook_form_alter() so messy? for more info).

In your case this should work:

$form['field_mage_category']['und'][0]['value']['#options'] = $arr;
  • your solution didnt work. The code you mentioned adds an '0' array to the field. but actual options is in the array field_mage_category->['und']['#options']. I am able to add values into this array and the values are getting displayed in the list as well but I the selected value is not getting displayed if the content is loaded again. Commented Apr 17, 2012 at 17:42
  • That's a little strange. What type of field/widget are you using for field_mage_category?
    – Clive
    Commented Apr 17, 2012 at 17:48
  • I am using list(text) field and Select list widget Commented Apr 17, 2012 at 18:14
  • The values are getting displayed in the list with both the codes I have specified in the question. Only thing is that it is not getting displayed when the content is loaded again. Commented Apr 17, 2012 at 18:17
  • 1
    So what is the solution to solve this? Commented Apr 18, 2012 at 8:36

For select field we can use as follows

$form['field_task']['und']['#options'] =$arr;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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