1

I have a views with grouped filters like this

Group 1 field_1_condition AND field_2_condition (exposed)

OR

Group 2 field_3_condition field_4_condition

I am expecting it to work like this: Filtering is done based on the first group and input comes from user to the exposed filter field. Then I want the query to check the second group and add all the matching things from there.

The initial query seems to be correct but as soon as I give input to exxposed filter the query is wrong. Views tries to combine similar fields for some reason

Initial query with those groups:

SELECT DISTINCT node.title AS node_title, node.nid AS nid, node.language AS node_language, og_membership.gid AS og_membership_gid, node.created AS node_created, 'node' AS field_data_field_event_date_node_entity_type
FROM 
{node} node
LEFT JOIN {og_membership} og_membership ON node.nid = og_membership.etid AND og_membership.entity_type = 'node'
WHERE (( (node.type IN  ('birthday_event')) AND (og_membership.gid IS NULL ) AND (node.status = '1') )OR( (node.status = '1') AND (node.type IN  ('event')) ))
ORDER BY node_created DESC

The query when exposed filter gets input

SELECT DISTINCT node.title AS node_title, node.nid AS nid, node.language AS node_language, og_membership.gid AS og_membership_gid, node.created AS node_created, 'node' AS field_data_field_event_date_node_entity_type
FROM 
{node} node
LEFT JOIN {og_membership} og_membership ON node.nid = og_membership.etid AND og_membership.entity_type = 'node'
WHERE (( (node.status = '1') AND (node.type IN  ('event', 'birthday_event')) AND (og_membership.gid = '1') ))
ORDER BY node_created DESC

This is the image of my views filter settings

Image of my views filter settings

1

I am still waiting for a response to do this more elegantly but since I am in a hurry I wrote a query alter to accomplish what I need. Maybe it's helpful to someone

function custom_views_query_alter(&$view, &$query) {
  if ($view->name == 'ts_group_events' && $view->current_display == 'calendar') {
    if (empty($query->where[2]['conditions'])) {
      foreach ($query->where[1]['conditions'] as &$condition) {
        if ($condition['field'] == 'node.type' && $key = array_search('birthday_event', $condition['value'])) {
          unset($condition['value'][$key]);
        }
      }
      $query->where[2]['conditions'][] = array(
        'field' => 'node.status',
        'value' => 1,
        'operator' => '='
      );

      $query->where[2]['conditions'][] = array(
        'field' => 'node.type',
        'value' => array('birthday_event'),
        'operator' => 'in'
      );

      $query->where[2]['conditions'][] = array(
        'field' => 'og_membership.gid',
        'value' => NULL,
        'operator' => 'IS NULL'
      );
    }
  }
}
  • Thanks Martin your answer helped me when I had the same problem of applying exposed filters to a second filter OR group. – Ben Jul 18 '18 at 8:32
0

I have the same issue in Drupal 8. You are able to configure the grouped conditions but they aren't filtering as they are supposed to. I believe this is an already documented issue: https://www.drupal.org/project/drupal/issues/2168893#comment-11791770

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.