1

I am attempting to add some "OR" statements to my Drupal view (to accomodate a grouping of taxonomy terms).

I am able to replace a tid one for one without an issue using the following code:

function customedm_views_query_alter(&$view, &$query) {
      if ($view->name == 'course_catalog') {
            dpm($query);
            foreach($query->where[1]['conditions'] as &$condition){
                if ($condition['value'] == 422){
                    $condition['value'] = 311;
                }
            }
        }
}

The problem is, I need to replace this value with several values (311,316,and 326)

I've tried replacing the $conditions variable with the proper or statement e.g.:

$condition = "WHERE field_data_field_course_subject.field_course_subject_tid = '311' OR field_data_field_course_subject.field_course_subject_tid = '326'";

Or creating a second $query->where[] array with the seemingly appropriate entries into the array:

function customedm_views_query_alter(&$view, &$query) {
      if ($view->name == 'course_catalog') {
            dpm($query);
            foreach($query->where[1]['conditions'] as &$condition){
                if ($condition['value'] == 422){            
                    $newconditions = array(
                    conditions => array(
                        array(
                            field => 'field_data_field_course_subject.field_course_subject_tid',
                            value => 311,
                            operator => '='
                            ),
                        array(
                            field => 'field_data_field_course_subject.field_course_subject_tid',
                            value => 326,
                            operator => '='
                            ),      
                        array(
                            field => 'field_data_field_course_subject.field_course_subject_tid',
                            value => 316,
                            operator => '='
                            )           
                        ),
                        args => array(),
                        type => "OR"
                    );
                    $query->where[] = $newconditions;
                }
            }
        }
}

Neither of these approaches seem to product the desired outcome (though I was able to verify that the array creating approach created the appropriate fields in $view->query... can someone show me how I can add OR statements to my query using this hook?

  • Just change the type for the where statement $query->where[0]['type'] = 'OR'; – iLLin Feb 14 '17 at 17:24
  • I need to keep that AND for the query to work properly-- node is published AND Node is of type AND Course Subject = 311 OR Course Subject = 316 OR Course Subject = 326 – dougblitz210 Feb 14 '17 at 17:29
  • 1
    $query->where[1]['conditions'][0]['field'] = "subject = 311 OR subject = 316"; (subject IN (311,316) maybe do that. I've done that before to get control. – iLLin Feb 14 '17 at 17:32
  • 1
    Did you try OR operator from the views filter ui? You can also do a grouping of filters from views ui itself. – arpitr Feb 14 '17 at 18:14
  • I've tried both: $condition['field'] = "WHERE field_data_field_course_subject.field_course_subject_tid = '311' OR field_data_field_course_subject.field_course_subject_tid = '326'"; and $condition['field'] = "field_data_field_course_subject.field_course_subject_tid IN (311,316,326)"; With no success. Grouping from within the views UI is not working for whatever reason (I started there). The OR operator from within the view itself won't really help me unless I want to add another display into the mix (I dont) --appreciate the thought though. – dougblitz210 Feb 14 '17 at 19:16
1

Thanks to iLLin I was able to figure out an acceptable answer to this question by using the in operator, and replacing the entire array for the condition.

function customedm_views_query_alter(&$view, &$query) {
  if ($view->name == 'course_catalog') {
        foreach($query->where[1]['conditions'] as &$condition){
            if ($condition['value'] == 422){            
                $newvalue = array(
                    field => 'field_data_field_course_subject.field_course_subject_tid',
                    value => array(311, 316, 326, 321),
                    operator => 'in'
                );                      


                $condition = $newvalue;
            }
        }
    }
}

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.