You are here

function _webform_select_options in Webform 5.2

Same name and namespace in other branches
  1. 6.3 components/select.inc \_webform_select_options()
  2. 6.2 components/select.inc \_webform_select_options()
  3. 7.4 components/select.inc \_webform_select_options()
  4. 7.3 components/select.inc \_webform_select_options()

Utility function to split user-entered values from new-line seperated text into an array of options.

Parameters

$text: Text to be converted into a select option array.

$flat: Optional. If specified, return the option array and exclude any optgroups.

6 calls to _webform_select_options()
theme_webform_mail_select in components/select.inc
Format the output of emailed data for this component.
_webform_analysis_rows_select in components/select.inc
Calculate and returns statistics about results for this component from all submission to this webform. The output of this function will be displayed under the "results" tab then "analysis".
_webform_csv_data_select in components/select.inc
Return the result of a textfield submission. The output of this function will be displayed under the "results" tab then "submissions".
_webform_csv_headers_select in components/select.inc
Return the header information for this component to be displayed in a comma seperated value file. The output of this function will be displayed under the "results" tab then "download".
_webform_render_select in components/select.inc
Build a form item array containing all the properties of this component.

... See full list

File

components/select.inc, line 471
Webform module multiple select component.

Code

function _webform_select_options($text, $flat = FALSE) {
  $options = array();
  $rows = array_filter(explode("\n", trim($text)));
  $group = NULL;
  foreach ($rows as $option) {
    $option = trim($option);

    /**
     * If the Key of the option is within < >, treat as an optgroup
     *
     * <Group 1>
     *   creates an optgroup with the label "Group 1"
     *
     * <>
     *   Unsets the current group, allowing items to be inserted at the root element.
     */
    if (preg_match('/^\\<([^>]*)\\>$/', $option, $matches)) {
      if (empty($matches[1])) {
        unset($group);
      }
      elseif (!$flat) {
        $group = _webform_filter_values($matches[1], NULL, NULL, FALSE);
      }
    }
    elseif (preg_match('/^([^|]+)\\|(.*)$/', $option, $matches)) {
      $key = _webform_filter_values($matches[1], NULL, NULL, FALSE);
      $value = _webform_filter_values($matches[2], NULL, NULL, FALSE);
      isset($group) ? $options[$group][$key] = $value : ($options[$key] = $value);
    }
    else {
      $filtered_option = _webform_filter_values($option, NULL, NULL, FALSE);
      isset($group) ? $options[$group][$filtered_option] = $filtered_option : ($options[$filtered_option] = $filtered_option);
    }
  }
  return $options;
}