You are here

function _form_type_options_value in Options Element 7

Same name and namespace in other branches
  1. 6 options_element.inc \_form_type_options_value()

Logic function for form_type_options_value(). Do not call directly.

See also

form_type_options_value()

1 call to _form_type_options_value()
form_type_options_value in ./options_element.module
This function adjusts the value of the element from a text value to an array.

File

./options_element.inc, line 258
All logic for options_element form elements.

Code

function _form_type_options_value(&$element, $edit = FALSE) {
  if ($edit === FALSE) {
    return array(
      'options' => isset($element['#options']) ? $element['#options'] : array(),
      'default_value' => isset($element['#default_value']) ? $element['#default_value'] : '',
    );
  }
  else {

    // Convert text to an array of options.
    $duplicates = array();
    $options = form_options_from_text($edit['options_field'], $element['#key_type'], empty($element['#optgroups']), $duplicates);

    // Convert default value.
    if (isset($edit['default_value_field'])) {

      // If the element supports toggling whether or not it will accept
      // multiple values, use the value that was passed in via $edit (keeping
      // in mind that this value may not be set, if a checkbox was used to
      // configure it). Otherwise, use the current setting stored with the
      // element itself.
      $multiple = !empty($element['#multiple_toggle']) ? !empty($edit['multiple']) : !empty($element['#multiple']);
      if ($multiple) {
        $default_value = array();
        $default_items = explode(',', $edit['default_value_field']);
        foreach ($default_items as $key) {
          $key = trim($key);
          $value = _form_options_search($key, $options, $element['#default_value_pattern']);
          if (!is_null($value)) {
            $default_value[] = $value;
          }
        }
      }
      else {
        $default_value = _form_options_search(trim($edit['default_value_field']), $options, $element['#default_value_pattern']);
      }
    }
    else {
      $default_value = NULL;
    }
    $return = array(
      'options' => $options,
      'default_value' => $default_value,
      'options_text' => $edit['options_field'],
    );
    if (isset($edit['default_value_field'])) {
      $return['default_value_text'] = $edit['default_value_field'];
    }
    return $return;
  }
}