11

I need to implement an OR operator between some filters in a Drupal View. By default, Drupal AND's every filter together.

By using

hook_views_query_alter(&$view, &$query)

I can access the query ( var $query ) , and I can change either :

$query->where[0]['type'] 

to 'OR', or

$query->group_operator 

to 'OR'

The problem is however, that I do not need OR's everywhere. I've tried changing both of them to OR seperately, and it doesn't yield the desired result.

It seems changing those values, puts OR's everywhere, while I need => ( filter 1 AND filter 2 ) OR ( filter 3 ), so just 1 OR.

I could just check the Query of the View, copy it, modify it, and run it through db_query, but that's just dirty ..

Any suggestions ?

Thx in advance.

3
  • As a second though to this, there is also using the query alter for the view -- I'm going to try this route. brianfending.com/content/…
    – cgp
    Feb 15, 2010 at 19:56
  • The query alter is what @Vodde is referring to as 'dirty', we are trying to modify the query clauses via a Views API call. Apr 22, 2011 at 2:51
  • 1
    Would be nice to specify Drupal and Views versions. Dec 15, 2011 at 6:51

4 Answers 4

12

If you are using Views 3 / Drupal 7 and looking for the answer to this question, it is baked right into Views. Where it says "add" next to filters, click the dropdown, then click "and/or; rearrange". It should be obvious from there.

2
  • +1 for this, It does exactly what I needed. also look at field groups once you get there. That was the only part that took me a few minutes to realize
    – Zach
    Nov 19, 2013 at 0:56
  • I have Views 3, and I don't see "and/or;" just "rearrange"?
    – Brittany
    May 26, 2016 at 22:02
7

Unfortunately this is still a missing feature in Views2. It has long been asked for and was promised a while ago, but seems to be a tricky piece of work and is now scheduled for Views3.

In the meantime you could try the Views Or module mentioned in that thread. As of today, it is still in dev status, but seems to be actively maintained and the issue queue does not look to bad, so you might want to give it a try.

1
  • +1 That this is not in Views2. $this->query->where[$group]['type'] = 'OR'; does not work, and $this->query->group_operator = 'OR'; converts all the operators between WHERE clauses to OR. Very frustrating. Apr 22, 2011 at 2:57
2

if you want do it with view_query_alter hook, you should use $query->add_where() where you can specify if it's AND or OR. From views/include/query.inc

  /**
   * Add a simple WHERE clause to the query. The caller is responsible for
   * ensuring that all fields are fully qualified (TABLE.FIELD) and that
   * the table already exists in the query.
   *
   * @param $group
   *   The WHERE group to add these to; groups are used to create AND/OR
   *   sections. Groups cannot be nested. Use 0 as the default group.
   *   If the group does not yet exist it will be created as an AND group.
   * @param $clause
   *   The actual clause to add. When adding a where clause it is important
   *   that all tables are addressed by the alias provided by add_table or
   *   ensure_table and that all fields are addressed by their alias wehn
   *   possible. Please use %d and %s for arguments.
   * @param ...
   *   A number of arguments as used in db_query(). May be many args or one
   *   array full of args.
   */
  function add_where($group, $clause)
1
  • 2
    I tried it myself and using this method results in ( filter 1 OR filter 2 ) AND ( filter 3 OR fliter 4 ), which is opposed to what we are looking for: ( filter 1 AND filter 2 ) OR ( filter 3 AND filter 4 ). Apr 22, 2011 at 2:53
1

I added it by concatenating the string.

It is relatively specific to the implementation - people would need to play with field to match for OR - node.title in the following code and the field to match it with - node_revisions.body in this case.

Extra piece of code to make sure that node_revisions.body is in the query.

/**
 * Implementation of hook_views_api().
 */
function eventsor_views_api() { // your module name into hook_views_api
  return array(
  'api' => 2,
  // might not need the line below, but in any case, the last arg is the name of your module
  'path' => drupal_get_path('module', 'eventsor'),
 );
}

/**
 *
 * @param string $form
 * @param type $form_state
 * @param type $form_id 
 */
function eventsor_views_query_alter(&$view, &$query) {

  switch ($view->name) {
    case 'Events':
      _eventsor_composite_filter($query);
      break;
  }
}

/**
 * Add to the where clause.
 * @param type $query 
 */
function _eventsor_composite_filter(&$query) {
  // If we see "UPPER(node.title) LIKE UPPER('%%%s%%')" - then add and to it.
  if (isset($query->where)) {

    $where_count = 0;
    foreach ($query->where as $where) {
      $clause_count = 0;
      if (isset($where['clauses'])) {
        foreach ($where['clauses'] as $clause) {
          $search_where_clause = "UPPER(node.title) LIKE UPPER('%%%s%%')";
          // node_data_field_long_description.field_long_description_value
          $desirable_where_clause = "UPPER(CONCAT_WS(' ', node.title, node_revisions.body)) LIKE UPPER('%%%s%%')";
          if ($clause == $search_where_clause) {
            //  $query->add_where('or', 'revisions.body = %s'); - outside of what we are looking for
            $query->where[$where_count]['clauses'][$clause_count] = $desirable_where_clause;

            // Add the field to the view, just in case.
            if (!isset($query->fields['node_revisions_body'])) {
              $query->fields['node_revisions_body'] = array(
                'field' => 'body',
                'table' => 'node_revisions',
                'alias' => 'node_revisions_body'
              );
            }
          }
          $clause_count++;
        }
      }
      $where_count++;
    }
  }
}

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.