3

How does one get the actual value for an exposed filter?

I can get the 'id' for a filter value using the following code and pulling the data out of the array, but this just returns integer values of 1,2,3,etc.:

$view = views_get_current_view();
$view->exposed_input['filter_name'];

using $view->get_exposed_input() results in the exact same issue.

How do I get the actual text string that is applied for the filter?

Specifically I'm trying to do this with a Global PHP field.

2
  • Can you confirm Drupal and Views versions? Think it actually matters here, as it will be slightly different in Drupal 7 vs Drupal 8. And, a really good answer for this question would show the method for both versions. :)
    – mpdonadio
    Commented Jul 28, 2016 at 0:01
  • Drupal 7, Views 3
    – Simo
    Commented Jul 28, 2016 at 0:02

2 Answers 2

2

The simplest way to do this is by using a Taxonomy to define your filter. By default, the 'IDs' returned in the array will be TIDs, which you can link back to Term name.

This post provides some easy instructions on how to use a Taxonomy as your filter (you possibly need to add it as a relationship in your view)

Get your IDs (values) the same way previously:

$view = views_get_current_view();
$view->exposed_input['filter_name'];

Then simply step through the array to pick up and match the IDs to the term name:

$view = views_get_current_view();
foreach ($view->exposed_input['filter_name'] as $tid) {
  $term = taxonomy_term_load($tid);
  $name = $term->name;
  print $name;
}

Boom!

0

To get both filter name and its value, you can do something like that :

Make a variable out of each filter

function MODULE_preprocess_exposed_filter_data(&$vars) {

  $view = $vars['view'];

  // In case no view - get the current view
  if ($view == NULL) {
    $view = views_get_current_view();
  }

  if (isset($view->exposed_input)) {
    foreach ($view->exposed_input as $filter => $value) {
      $vars[$filter] = $value;
      if (is_array($value)) { 
        $vars['exposed_filters'][$filter] = check_plain(implode(", ", $value));
      } else {
        $vars['exposed_filters'][$filter] = check_plain($value);
      }
    }
  }
}

Declare your template

function MODULE_theme() {
  return array(
    'exposed_filter_data' => array(
      'template' => 'exposed_filter_data',
      'variables' => array(
        'view' => NULL,
      ),
    ),
  );
}

Call to template output

function get_exposed_filter_output() {
  return theme('exposed_filter_data');
}

In exposed_filter_data.tpl.php file

<?php if (isset($exposed_filters)): ?>
  <div class="exposed_filter_data">
    <div class="title"><?php print t('Filtered by:'); ?></div>
    <div class="content">
      <?php foreach ($exposed_filters as $filter => $value): 
      //Do stuff with your filters and values here ?>
        <?php if ($value): ?>

        //taxonomy terms for instance
        if ($filter == 'field_taxonomy_tid') : 
          if($value != '' && $value != 'All' ){ 

            $term = taxonomy_term_load($tid);
            $vocab = taxonomy_vocabulary_load($term->vid);
            $vocab_name = $vocab->name; ?>

            <div class="filter">
              <div class="name">$vocab_name :</div>
              <div class="value">
                <?php $types = explode(',', $value);
                $t = taxonomy_term_load_multiple ($types);
                $exp_types = '';
                foreach ($t as $k=>$v) {

                  $exp_types .= $v-> name . ', ';                

                }
                $exp_types = substr($exp_types, 0, -2);
                print $exp_types;
                ?>
              </div>
            </div>
            <?php }
          endif; 
        <?php endif; ?>
      <?php endforeach; ?>
    </div>
  </div>
<?php endif; ?>

Finally print your exposed filter data : instead of using global PHP in your view, you'd better try something like that :

function MODULE_views_pre_render(&$view) {

  if ( $view->name == 'MYVIEWNAME' ){
    //display exposed filters in a custom text area
    if (isset($view->header['area_text_custom'])){
      $exp = get_exposed_filter_output();
      $view->header['area_text_custom']->options['content'] = $exp;
    }
  }
}

This is 80% inspired by this little handy module : Exposed Filter Data

1
  • Thanks @Kojo for the comprehensive answer. I haven't tried yours but I stumbled upon a solution playing with Taxonomies to define the filter.
    – Simo
    Commented Jul 28, 2016 at 22:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.