You are here

function cck_select_other_pre_render in CCK Select Other 6

Same name and namespace in other branches
  1. 7 cck_select_other.module \cck_select_other_pre_render()

Pre render callback for the form so we can recreate the fake form after it gets blown away by the CCK process callback.

Parameters

$form the form:

Return value

$form

1 string reference to 'cck_select_other_pre_render'
cck_select_other_elements in ./cck_select_other.module
Implementation of hook_elements

File

./cck_select_other.module, line 208
Implements a select list widget that lets a user provide an alternate option.

Code

function cck_select_other_pre_render($form) {
  static $js;
  if (isset($form['select_other_list'])) {
    return $form;
  }
  $field_name = substr($form['#field_name'], 6);
  if (!$js) {
    drupal_add_js(array(
      'CCKSelectOther' => array(
        'field' => $field_name,
      ),
    ), 'setting');
    drupal_add_js(drupal_get_path('module', 'cck_select_other') . '/cck_select_other.js');
    $js = TRUE;
  }
  else {
    drupal_add_js(array(
      'CCKSelectOther' => array(
        'field' => $field_name,
      ),
    ), 'setting');
  }
  unset($form['#default_value']);
  unset($form['#value']);
  $field = content_fields($form['#field_name']);
  $def = $form['#post'][$form['#field_name']]['select_other_text_input'];
  $select_def = $form['#post'][$form['#field_name']]['select_other_list'];
  $delta = $form['#delta'];
  $options = cck_select_other_options($field);
  $new_form = array(
    '#type' => 'fieldset',
    '#title' => t($field['widget']['label']),
    '#description' => $form['#description'],
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
    '#delta' => $form['#delta'],
    '#field_name' => $form['#field_name'],
    '#type_name' => $form['#type_name'],
    '#tree' => $form['#tree'],
  );
  $new_form['select_other_list'] = array(
    '#id' => 'edit-field-' . $field_name . '-select-other-list',
    '#name' => $form['#field_name'] . '[select_other_list]',
    '#parents' => array(
      'cck_select_other',
    ),
    '#type' => 'select',
    '#title' => t(''),
    '#description' => t('Select other to enter your own option.'),
    '#options' => $options,
    '#default_value' => $select_def,
    '#value' => $select_def,
    '#weight' => -10,
  );
  $new_form['select_other_text_input'] = array(
    '#id' => 'edit-field-' . $field_name . '-select-other-text-input',
    '#name' => $form['#field_name'] . '[select_other_text_input]',
    '#parents' => array(
      'cck_select_other',
    ),
    '#type' => 'textfield',
    '#title' => t('Specify other'),
    '#default_value' => $select_def == 'other' ? check_plain($def) : '',
    '#value' => $select_def == 'other' ? check_plain($def) : '',
  );
  return $new_form;
}