You are here

function global_filter_form in Views Global Filter 6

Creates the drop-down selector for the global selector field.

1 string reference to 'global_filter_form'
global_filter_forms in ./global_filter.module
Implements hook_forms();

File

./global_filter.module, line 149
global_filter.module

Code

function global_filter_form(&$form_state, $filter) {
  $field_name = variable_get($filter, '');
  $option_all_text = variable_get($filter . '_option_all_text', '');
  if (empty($option_all_text)) {
    $option_all_text = GLOBAL_FILTER_DEF_OPTION_ALL_TEXT;
  }
  if (drupal_substr($field_name, 0, 4) == 'view') {
    $view_name = drupal_substr($field_name, 5);
    $options = $option_all_text == '<none>' ? array() : array(
      '' => check_plain($option_all_text),
    );
    global_filter_add_view_results($options, $view_name);
    $is_list = TRUE;
  }
  elseif (!in_array($field_name, array_keys(global_filter_get_node_properties()))) {
    $field = NULL;

    //@tobeported: field_info_field($field_name);
    if (!$field) {
      if ($field_name) {
        drupal_set_message(t('The field %name used for %filter does not exist. Please re-configure the associated Global Filter block.', array(
          '%name' => $field_name,
          '%filter' => $filter,
        )), 'error');
      }
      return;
    }
    $is_list = drupal_substr($field['type'], 0, 4) == 'list';
    if ($field['type'] == 'taxonomy_term_reference') {
      $vocabulary_name = $field['settings']['allowed_values'][0]['vocabulary'];
    }
    $instance = global_filter_get_field_instance($field_name);
    $is_autocomplete = $instance['widget']['type'] == 'taxonomy_autocomplete';
    if (!$is_autocomplete) {
      if ($is_list || $vocabulary_name) {
        $options = $option_all_text == '<none>' ? array() : array(
          '' => check_plain($option_all_text),
        );
      }
      if ($vocabulary_name) {
        _global_filter_add_terms($options, $vocabulary_name);
      }
      elseif ($is_list) {

        // Not using array_merge() as it re-indexes when the keys are integers
        foreach (list_allowed_values($field) as $key => $value) {
          $options[$key] = $value;
        }
      }
    }
  }
  $path = drupal_get_path('module', 'global_filter');

  // Note if already included, drupal_add_css() won't do anything.
  drupal_add_css($path . '/global_filter.css');
  $default_value = isset($form_state['values'][$field_name]) ? $form_state['values'][$field_name] : $_SESSION['global_filter'][$field_name];
  $form['#attributes'] = array(
    'class' => 'global-filter',
  );
  if ($is_autocomplete) {
    foreach (taxonomy_get_vocabularies() as $vid => $vocabulary) {
      if ($found_vocabulary = $vocabulary->machine_name == $vocabulary_name) {
        break;
      }
    }
    if (!empty($found_vocabulary)) {
      $tids = explode('+', $default_value);

      // only support OR/+ for tids
      if (!empty($tids) && is_numeric($tids[0])) {

        // If the first is numeric, assume all are numeric, i.e. tids.
        // @tobeported
        $terms = taxonomy_term_load_multiple($tids);
      }
      else {

        // Not supporting + on term names
        // @tobeported
        $conditions = array(
          'vid' => $vid,
          'name' => $default_value,
        );
        $terms = taxonomy_term_load_multiple(array(), $conditions);
      }
      if (!empty($terms)) {
        $_SESSION['global_filter'][$field_name] = implode('+', array_keys($terms));
        $term_names = array();
        foreach ($terms as $term) {
          $term_names[] = $term->name;
        }
      }
    }
    $default_value = empty($term_names) ? NULL : implode('+', $term_names);
    $form[$field_name] = array(
      '#type' => 'textfield',
      '#default_value' => $default_value,
      '#autocomplete_path' => $instance['widget']['settings']['autocomplete_path'] . '/' . $field_name,
      '#size' => $instance['widget']['settings']['size'],
      '#maxlength' => 1024,
    );
  }
  else {
    $form[$field_name] = array(
      '#title' => '',
      '#type' => $is_list || $vocabulary_name ? 'select' : 'textfield',
      '#default_value' => $default_value,
    );
    if ($options) {
      $form[$field_name]['#options'] = $options;
    }
  }
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Set'),
    '#submit' => array(
      'global_filter_set_value_on_session',
    ),
  );
  if ($form[$field_name]['#type'] == 'select' && variable_get('global_filter_set_on_select', FALSE)) {

    // Add javascript to auto-submit on select.
    drupal_add_js($path . '/global_filter.js');

    // Suppress the above 'Set' submit button.
    $form['submit']['#attributes'] = array(
      'style' => 'display:none',
    );
  }

  // Pass $field_name to global_filter_set_value_on_session()
  $form_state['global_filter_name'] = $field_name;

  // does not work in D6
  return $form;
}