You are here

function cck_select_other_field_widget_form in CCK Select Other 7

Same name and namespace in other branches
  1. 7.2 cck_select_other.module \cck_select_other_field_widget_form()

Implementation of hook_field_widget_form().

File

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

Code

function cck_select_other_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
  $options = cck_select_other_options($instance);
  $def = $instance['required'] ? '' : '_none';
  $otherdef = '';
  if (empty($items)) {
    $items[] = array(
      'value' => '',
    );
  }
  if (!isset($items[$delta])) {
    $items[$delta] = array(
      'value' => '',
    );
  }

  // Set default value if we have instance data for delta, a default
  // value setting, or something basic if none at all.
  if (!empty($items[$delta]['value'])) {
    $def = in_array($items[$delta]['value'], array_keys($options)) ? $items[$delta]['value'] : 'other';
    $otherdef = $def == 'other' ? $items[$delta]['value'] : '';
  }
  else {
    if (isset($instance['default_value'])) {
      $def = isset($instance['default_value'][0]['select_other_list']) ? $instance['default_value'][0]['select_other_list'] : 'other';
      $otherdef = $def == 'other' ? $instance['default_value'][0]['value'] : '';
    }
  }

  // This needs to be pulled out of our original element.
  $description = $element['#description'];
  $element = array(
    '#type' => $instance['widget']['type'],
    '#default_value' => '',
    '#prefix' => '<div id="field-' . substr($field['field_name'], 6) . '-' . $langcode . '-' . $delta . '-wrapper">',
    '#suffix' => '</div>',
    '#field_name' => $field['field_name'],
    // Required fields for field_conditional_state.
    '#field_parents' => isset($form['#parents']) ? $form['#parents'] : array(),
    '#bundle' => $instance['bundle'],
  );
  $element['select_other_list'] = array(
    '#title' => check_plain($instance['label']),
    '#description' => !empty($description) ? $description : t('You may specify your own option.'),
    '#type' => 'select',
    '#options' => $options,
    '#default_value' => $def,
    '#attributes' => array(
      'class' => array(
        'form-text form-select select_other_list',
      ),
    ),
    '#required' => $instance['required'],
  );

  // @fixme - #states is REALLY slow here so I'm using my own shit again.
  $element['select_other_text_input'] = array(
    '#type' => 'textfield',
    '#default_value' => $otherdef,
    '#attributes' => array(
      'class' => array(
        'form-text select_other_text_input',
      ),
    ),
    '#element_validate' => array(
      'cck_select_other_widget_validate',
    ),
  );
  return $element;
}