I have created a custom element through a module. This custom element is a drop down element which reads the values from an array saved in the variable table, which in turn is controlled by a configuration menu (also part of the new module). This all works fine as a stand alone element and would work fine on a new single page form, however the forms I need to implement this element on are multi page forms with an existing hard coded drop down which I am replacing with my new drop down.

The main issue that I am experiencing is that the '#default_value' that is set in the code that creates the form, is not being applied to the new element, I have tried setting this in the form creation script and as a 'hook_form_alter' function, but both of these methods seem to be run before the new element is created. The reason for needing a default value is because the form is a multipage form, which users will be able to click back to previous pages on and edit their previously entered inputs, so when a user clicks back to a previous page, they should be presented with their previously selected title on the drop down field.

Is anyone able to offer me any advice as to how I can set these default values.

The element is called as part of the form creation code using the array entry below:

$form['title'] = array(
  '#title' => t('Title'),
  '#description' => t('Select the delegate\'s title from the drop-down list.'),
  '#type' => 'title_select',
  '#required' => TRUE,
  '#default_value' => !empty($form_state['values']['title']['titledropdown']) ? $form_state['values']['title']['titledropdown'] : NULL,
  '#input' => TRUE,

The code to create the custom element which is output is within a custom module:

function person_title_titledropdown_process($element, &$form_state, $complete_form) {

  $title_var_array = variable_get('form_person_title', array());
  $title_array = array();
  foreach($title_var_array AS $k_title => $v_title){
    $title_array[t($v_title['name'])] = t($v_title['name']);

  $element['#tree'] = TRUE;
  $element['titledropdown'] = array(
    '#type' => 'select',
    '#options' => $title_array,
    '#required' => $element['#required'],
  if (isset($element['#attributes'])) {
    $element['titledropdown']['#attributes'] = $element['#attributes'];
  return $element;

The relevant portion of the content of the $form_state['values'] array is shown below:

Array ( 
  [title] => Array ( 
    [titledropdown] => Lord 
  [firstname] => Test 
  [surname] => User 
  [email] => test@example.com 
  [telephone_number] => 01234 567890 
  [mobile_number] => 
  [event_code] => 
  [campaign_code] => 
  [terms] => 1 
  [delegate_list] => 0 
  [del_first] => 0 
  [next] => Next 
  [form_build_id] => form-Gsxu27DA8V1v2hz9_q9TBxiKsUwFEzUiXjqy_QTEZa8 
  [form_token] => OYFyuoo8yToYkHZTzh76-rN2EZLaAukwpV2SxZFjV1w 
  [form_id] => events_registration_form 

The html for the drop down field is output as:

<select class="form-select required" name="title[titledropdown]" id="edit-title-titledropdown" original-title="">
  <option value="">- Select -</option>
  <option value="Mr">Mr</option>
  <option value="Mrs">Mrs</option>
  <option value="Miss">Miss</option>
  <option value="Dr">Dr</option>
  <option value="Prof">Prof</option>
  <option value="Lord">Lord</option>
  <option value="Sir">Sir</option>
  • 1
    The only place you are supposed to alter form elements is hook_form_alter and it's family. Adding elements in a processing or theming functions will not make them work ad Form API elements and thus will not work with default values, validators and submit handlers. – Mołot Dec 23 '13 at 12:13

Your Answer

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

Browse other questions tagged or ask your own question.