You are here

function filter_harmonizer_convert_contextual_to_regular_value in Views Filter Harmonizer 7

Convert a supplied contextual filter value to an equivalent exposed value.

Parameters

object $contextual_filter: the contextual_filter object

object $regular_filter: if the exposed filter is grouped, this will contain the group mapping

string $value: the value, can be a string, number or array of tids or grouped filter ids.

1 call to filter_harmonizer_convert_contextual_to_regular_value()
filter_harmonizer_views_exposed_form_submit in ./filter_harmonizer.module
Supplementary submit handler for 'views_exposed_form'.

File

./filter_harmonizer.module, line 430
filter_harmonizer.module For Views where both exposed and contextual filters are active on a page.

Code

function filter_harmonizer_convert_contextual_to_regular_value($contextual_filter, $regular_filter, $value) {
  if (!isset($contextual_filter) || !isset($regular_filter)) {
    return FALSE;
  }
  $is_a_group = $regular_filter
    ->is_a_group();
  $field_name = $is_a_group ? $regular_filter->options['group_info']['identifier'] : $regular_filter->options['expose']['identifier'];
  $last_value =& drupal_static(__FUNCTION__, array());
  if (empty($value) && isset($last_value[$field_name])) {

    // This happens in the case of exposed filters in blocks. For reasons I
    // don't understand they get set twice, the second time with an empty value.
    $value = $last_value[$field_name];
  }
  else {
    $last_value[$field_name] = $value;
  }

  // Deal with multi-select boxes for lists and taxonomy terms.
  if (is_a($contextual_filter, 'views_handler_argument_numeric') || filter_harmonizer_is_tax_filter($contextual_filter)) {

    // If the contextual filter has a "Taxonomy term" validator specified of
    // type "Term name converted to Term ID", then $value will hold term
    // names, not numeric tids.
    $ids = array();
    foreach (explode('|', $value) as $val) {
      if (is_numeric($val)) {
        $ids[] = $val;
      }
      else {

        // Check if it's a contextual arg validator with spaces replaced by
        // dashes; then do the reverse.
        if (!empty($contextual_filter->options['validate_options']['transform'])) {
          $val = str_replace('-', ' ', $val);
        }

        // Should return only one, unless the name is ambiguous.
        foreach (taxonomy_get_term_by_name($val) as $term) {
          $ids[] = $term->tid;
        }
      }
    }
    $value = $ids;
  }
  else {

    // Check if it's a contextual arg with spaces replaced by dashes. If so
    // then reverse. Cannot work in combination with "Alllow multiple values",
    // as both options rely on spaces.
    if (!empty($contextual_filter->options['transform_dash'])) {
      $value = str_replace('-', ' ', $value);
    }
  }
  return $is_a_group ? filter_harmonizer_get_grouped_filter_options($value, $regular_filter) : $value;
}