1

I have a content type teams and a content type games. Games has two node reference fields: home-team and away-team (that both reference content type teams). I want to create a single exposed filter teams that filters to games where either home-team or away-team matches the selected team.

For those with more experience hacking views - what approach would you take? Should I extend a handler to do this (views_handler_filter_in_operator)? Any pointers?

Or run a db_query and pass the results to a views alter hook?

Any input appreciated.

1

Views 3 views have a neat little semi-hidden feature. Click the arrow on the add button in the Filters section and pick and/or. From here, you can create filter groups and sort your filters to use AND or OR depending on your needs. Play with it; I think you'll be able to solve your issues like this.

  • This doesn't seem to work for my use case - I need to create a single filter that searches in both fields... The *or functionality seems to simply filter the view based on whether the condition of either filter is met... – starsinmypockets Oct 31 '11 at 11:41
  • Ah, good point. Next two things to check would be 1) if you can use contextual filters in conjunction with the exposed filter somehow and 2) if Views PHP lets you expose the filter and perhaps control the choices the user has. Sorry that my answer didn't quite address it, but hope this helps. – wizonesolutions Nov 1 '11 at 0:30
1

I realize this is an old post, but for those not wanting to write custom code, the Views filters populate module handles this nicely. It allows you to populate the values of two or more filters with just one exposed filter.

For @stars' use case, you'd:

  1. Add a filters for the home-team and away-team fields.
  2. Choose "and/or, rearrange" from the "Filter Criteria" section and create a new filter group (use "AND" for the group operator).
  3. Drag both fields into the new group and "OR" them.
  4. Add a "Global: Populate filters" filter.
  5. Expose the new filter, label it "Teams", and check the boxes of the filters you want it to populate (home-team and away-team).
  6. You may need to double-check that your new filter is part of the original AND-ed filter group. It should looks something like this:

    FILTER CRITERIA
    [other filters if you have them...] AND
    Teams (exposed)
    AND
    home-team OR
    away-team
    
1

From the views filters populate module page:

Since Views 7.x-3.4, there is a "Global: combine field filter" built-in filter in Views, so you may not need this module.

  • wow, that's a cool new feature, thanks a bunch for posting! You saved my day! ;-) – uwe Mar 10 '13 at 22:06
0

The answer, for me, did end up to be to extend one of the views handlers.

Here's the sauce:

<?php
/*
 * Shows results if team is referenced by field_data_field_home_team or field_data_field_away_team
 */
class views_handler_filter_mtairy_games extends views_handler_filter_in_operator {

  /**
   * This kind of construct makes it relatively easy for a child class
   * to add or remove functionality by overriding this function and
   * adding/removing items from this array.
   */
  function operators() {
    $operators = array(
      'in' => array(
        'title' => t('Is one of'),
        'short' => t('in'),
        'short_single' => t('='),
        'method' => 'op_simple',
        'values' => 1,
      ),
      'not in' => array(
        'title' => t('Is not one of'),
        'short' => t('not in'),
        'short_single' => t('<>'),
        'method' => 'op_simple',
        'values' => 1,
      ),
      'custom' => array(
        'title' => t('Custom'),
        'short' => t('custom'),
        'short_single' => t('<>'),
        'method' => 'op_custom',
        'values' => 1,
      ),
    );
    // if the definition allows for the empty operator, add it.
    if (!empty($this->definition['allow empty'])) {
      $operators += array(
        'empty' => array(
          'title' => t('Is empty (NULL)'),
          'method' => 'op_empty',
          'short' => t('empty'),
          'values' => 0,
        ),
        'not empty' => array(
          'title' => t('Is not empty (NOT NULL)'),
          'method' => 'op_empty',
          'short' => t('not empty'),
          'values' => 0,
        ),
      );
    }

    return $operators;
  }

  function op_custom() {
    if (empty($this->value)) {
      return;
    }
    $this->ensure_my_table();

    $this->query->add_table('field_data_field_away_team');
    $this->query->add_where_expression($group = '', $snippet = '(field_data_field_home_team.field_home_team_nid IN (:1)OR field_data_field_away_team.field_away_team_nid IN (:1))', $args = array(':1' => $this->value));
  }

}

Once I got views to implement the handler, I just exposed the filter and was golden.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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