i have a view with an exposed term filter. I need to insert other (non-term) values into the select list and programmatically process when one of these fake selections is submitted.

by example:
terms:
Spring 2013
Fall 2013
Winter 2014
Spring 2014

but insert (via form alter):
2013
2014

so i now end up with:
2013
- Spring 2013
- Fall 2013
2014
- Winter 2014
- Spring 2014

tricky part is that if user selects 2014, i will automatically be submitting the tids for Winter 2014 and Spring 2014.

i can use form_alter to modify the select list and:
if i set my exposed filter to accept multiple selections
- then i can use hook_views_pre_build and $_GET['field_term_tid'] = $tids; to set multiple tid vaues and this all works.. :)

but, i need this to be a single select field
- then this no longer works

is there some other views hook that i can use to manipulate this after views has blocked my multiple selections for a single only allowed filter?

Comments

liquidcms’s picture

only way i could figure to do this was to add a #pre_render to the form and manipulate the field parameters there.

so partial code snippets look like this:


function umd_form_alter(...) {
.
.
.
    case 'views_exposed_form':
      // form alter is hit twice; so lets check if this is the 2nd time and just skip if it is
      if (strlen(current($form['field_term_tid']['#options'])) == 4) break;
      $current = 0;
      foreach ($form['field_term_tid']['#options'] as $key => $option) {
        $yr = substr($option, -4, 4);
        if ($yr != $current) {
          $options[$yr] = $yr;
          $current = $yr;
        }
        $options[$key] = ' - ' . $option;
      }     
      $form['field_term_tid']['#options'] = $options;
      $form['field_term_tid']['#pre_render'][] = '_umd_exposed_pre_render';
      
      break;
  }
}

function umd_views_pre_build(&$view) {
  $_POST['original_tid'] = $_GET['field_term_tid'][0];
  if ($_GET['field_term_tid'][0] > 2000) {
    $year = $_GET['field_term_tid'][0];
    $tree = taxonomy_get_tree(TERM_VID);
    foreach ($tree as $term) {
      if (stristr($term->name, $year)) {
        $tids[$term->tid] = $term->tid;
      }
    }
    $_GET['field_term_tid'] = $tids; //implode('+', $tids);
  }
  else if ($view->display[$view->current_display]->handler->handlers['filter']['field_term_tid']) {
    $current = _umd_get_current_term();
    $view->display[$view->current_display]->handler->handlers['filter']['field_term_tid']->value = array($current => $current);
  }
  return;  
}

function _umd_exposed_pre_render($element) {
  unset($element['#attributes']['multiple']);
  $element['#multiple'] = false;
  $element['#size'] = 1;
  $element['#value']= $_POST['original_tid'];
  return $element;  
}

the else if part in the pre build is used to set a default value; but this no longer works... likely due to my pre_render code.

ycshen’s picture

liquidcms’s picture

interesting modules; but nope, none of those would help.. :)

MustangGB’s picture

Issue summary: View changes
Status: Active » Fixed

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.