You are here

function _autocomplete_widgets_get_options_allowvals in Autocomplete Widgets for Text and Number Fields 7

Same name and namespace in other branches
  1. 6 autocomplete_widgets.common.inc \_autocomplete_widgets_get_options_allowvals()

Fetch an array of options for the given widget (allowed values).

Options are retrieved from the allowed values defined for the field.

1 call to _autocomplete_widgets_get_options_allowvals()
_autocomplete_widgets_get_options in ./autocomplete_widgets.common.inc
Fetch an array of options for the given widget.

File

./autocomplete_widgets.common.inc, line 67
Common functions for Autocomplete Widgets module.

Code

function _autocomplete_widgets_get_options_allowvals($instance, $string = '', $match = 'contains', $keys = NULL, $limit = NULL) {
  $field_name = $instance['field_name'];
  $allowed_values = list_allowed_values(field_info_field($field_name));
  $limit = !isset($limit) || !is_numeric($limit) ? count($allowed_values) : $limit;
  $case_sensitive = $instance['widget']['settings']['autocomplete_case'];
  $filter_xss = !empty($instance['widget']['settings']['autocomplete_xss']);
  $options = array();
  $count = 0;

  //@todo: cant the count var be replaced with a call to count()?
  foreach ($allowed_values as $key => $value) {
    if ($filter_xss) {

      // Filter all HTML in $value, then trim white spaces.
      $value = trim(filter_xss($value, array()));
    }
    if ($string === '') {
      if (isset($keys) && is_array($keys)) {
        if (in_array($key, $keys)) {
          $options[$key] = $value;
          $count++;
        }
      }
      else {
        $options[$key] = $value;
        $count++;
      }
    }
    else {
      if ($match == 'equals') {
        if ($value == $string) {
          $options[$key] = $value;
          $count++;
        }
      }
      else {
        $pos = $case_sensitive ? strpos($value, $string) : strpos(drupal_strtolower($value), drupal_strtolower($string));
        if ($match == 'starts_with' && $pos === 0 || $match == 'contains' && $pos !== FALSE) {
          $options[$key] = $value;
          $count++;
        }
      }
    }
    if ($count >= $limit) {
      break;
    }
  }
  _autocomplete_widgets_sort_options($options, $instance);
  return $options;
}