You are here

function global_filter_set_instance_widget_value in Views Global Filter 8

Same name and namespace in other branches
  1. 7 global_filter.widgets.inc \global_filter_set_instance_widget_value()

Set widget instale value.

1 call to global_filter_set_instance_widget_value()
global_filter_create_field_instance_widget in ./global_filter.widgets.inc
Cast widget in the mould of the widget configured for the supplied's field.

File

./global_filter.widgets.inc, line 437
global_filter.widgets.inc

Code

function global_filter_set_instance_widget_value($session_value, $field, $form_state) {
  $name = $field['field_name'];
  $lang = $form_state['language'];
  $form_input = isset($form_state['input'][$name][$lang]) ? $form_state['input'][$name][$lang] : NULL;
  $form_values = isset($form_state['values'][$name][$lang]) ? $form_state['values'][$name][$lang] : NULL;
  if (is_array($form_input)) {
    $value = $form_input;
  }
  if (is_array($form_values)) {

    // Usually this means an autocomplete taxonomy, retrieve the term id.
    $form_values = reset($form_values);
    $value = empty($form_values['tid']) ? $form_values['name'] : $form_values['tid'];
  }
  if (empty($value)) {
    $value = $session_value;
  }

  // $items is used to set defaults. Must use this format (multi-valued):
  // $items[0][$key] = 4
  // $items[1][$key] = 23
  // where $key tends to equal 'value' or 'tid'
  $items = array();
  $key = key($field['columns']);
  if (!empty($value) && is_array($value)) {
    foreach (global_filter_array_flatten($value) as $value) {
      $items[] = array(
        $key => $value,
      );
    }
  }
  else {
    $items[0][$key] = $value;
  }

  // If nothing set, return something that represents 'All'. Otherwise the
  // field widget will set the instance default.
  return empty($items) ? array(
    0 => array(),
  ) : $items;
}