You are here

function select_or_other_cck_validate in Select (or other) 6.2

Same name and namespace in other branches
  1. 6 select_or_other.module \select_or_other_cck_validate()

Element validate callback for a Select (or other) CCK widget.

1 string reference to 'select_or_other_cck_validate'
select_or_other_widget in ./select_or_other.module
Implementation of hook_widget().

File

./select_or_other.module, line 574
The Select (or other) module.

Code

function select_or_other_cck_validate($element, &$form_state) {
  $other_selected = FALSE;
  if (is_array($element['select']['#value']) && isset($element['select']['#value']['select_or_other'])) {

    // This is a multiselect. assoc arrays
    $other_selected = TRUE;
    $value = $element['select']['#value'];
    unset($value['select_or_other']);
    $value[$element['other']['#value']] = $element['other']['#value'];
  }
  else {
    if (is_string($element['select']['#value']) && $element['select']['#value'] == 'select_or_other') {

      // This is a single select.
      $other_selected = TRUE;
      $value = $element['other']['#value'];
    }
    else {
      $value = $element['select']['#value'];
    }
  }
  if ($other_selected && !$element['other']['#value'] && $form_state['values']['form_id'] != 'content_field_edit_form') {
    form_error($element['other'], t('%name: @title is required', array(
      '%name' => t($element['select']['#title']),
      '@title' => $element['#other'],
    )));
  }
  if (isset($value)) {
    if (in_array($element['#cck_widget'], array(
      'select_or_other',
      'select_or_other_buttons',
    ))) {

      // Filter out 'none' value (if present, will always be in key 0)
      if (isset($items[0]['value']) && $items[0]['value'] === '') {
        unset($items[0]);
      }
      if ($element['#multiple'] >= 2 && count($value) > $element['#multiple']) {
        form_error($element['select'], t('%name: this field cannot hold more than @count values.', array(
          '%name' => t($element['select']['#title']),
          '@count' => $element['#multiple'],
        )));
      }
      $delta = 0;
      $values = array();
      foreach ((array) $value as $v) {
        $values[$delta++]['value'] = $v;
      }
      $value = $values;
    }
    else {
      if ($element['#cck_widget'] == 'select_or_other_sort') {
        $value = array(
          'value' => $value,
        );
      }
    }
    form_set_value($element, $value, $form_state);
    $form_state['clicked_button']['#post'][$element['#name']] = $value;

    // Is this something we should do?
  }

  // Add values to available options is configured to do so.
  $field_name =& $element['#parents'][0];
  $type =& $form_state['values']['type'];
  module_load_include('inc', 'content', 'includes/content.crud');
  $instance = reset(content_field_instance_read(array(
    'type_name' => $type,
    'field_name' => $field_name,
  )));
  if ($instance['widget']['other_unknown_defaults'] == 'available') {
    if (($element['select']['#value'] == 'select_or_other' || is_array($element['select']['#value']) && isset($element['select']['#value']['select_or_other'])) && !empty($element['other']['#value']) && !isset($element['#options'][$element['other']['#value']])) {

      // Make the change.
      $instance['widget']['available_options'] .= "\n" . $element['other']['#value'];

      // Save the instance.
      content_field_instance_update($instance);
    }
  }
  return $element;
}