1

My question is rather simple and I have searched quite a bit for the answer with no result. My view produces below SQL (simplified):

SELECT stuff
FROM 
tableA
LEFT JOIN tableB
LEFT JOIN tableC ON stuff
WHERE (( (MY_CONTEXTUAL_FILTER = '5' ) )AND(( (MY_NORMAL_FILTER IS NULL ) )))

All I want to do is to change that AND between the contextual filter and normal filter. I have tried to change all ANDs (including set_group_operator()) to OR and it does not change my AND.

Also need to mention it is an 'Entity Reference' display which I am trying to achieve the above on. Views version 7.x-3.7.

UPDATE

In order to achieve the above, I also tried hook_views_pre_execute() as below:

 $old_query = (string)$view->query->query();
 $new_query = str_replace('AND', 'OR', $old_query);
 $view->build_info['query']=$new_query;

But that doesn't work and throws and error. I know they are different approaches with different issues but both are trying to solve one problem.

2 Answers 2

2
+50

As a simple try to help, did you implement hook_views_query_alter(&$view, &$query) as following ?

First, call Views API in YOUR_MODULE.module :

/**
 * Implementation of hook_views_api().
 */
function YOUR_MODULE_views_api() { 
  return array(
    'api' => 3,
    'path' => drupal_get_path('module', 'YOUR_MODULE_NAME'),
  );
}

Create a file YOUR_MODULE.views.inc. It seems that with Views, you don't need to put files[] = YOUR_MODULE.views.inc in your module's .info file, as you usually would.

Then implement hook_views_query_alter() in views.inc.

function YOUR_MODULE_views_query_alter(&$view, &$query) {

  if($view->name == 'VIEW_NAME') {

    dpm($query);
    //at this point find the right part to alter and adapt following line
    $query->where[0]['type'] = 'OR';

  }
}

EDIT: If for any reason, you can't override the View with hook_views_query_alter(), Views Filter Harmonizer might fit your need.

Views Filter Harmonizer solves an operational foible with the Views module regarding filtering. This small module allows you to have a contextual filter argument apply only when there is no corresponding regular (exposed) filter value present.

Last, check also Views arguments in filters sandbox, or test this patch. Both rely on the same logic, setting a "Global: Null" contextual filters and using it in regular filters. Many people report using it to solve the same And/Or contextual filters issue. Good luck !

3
  • The second part YOUR_MODULE_views_query_alter is how I have it but I don't understand why I need the YOUR_MODULE_views_api as I am using Drupal 7.x and Views 3.7 ?
    – Mazzi
    Aug 29, 2014 at 13:21
  • With hook_views_query_alter INTO .module and no hook_views_api() at all it works. With hook_views_query_alter INTO YOUR_MODULE.views.inc and no hook_views_api() at all it doesn't work. I believe the cleanest way to do that seems to put views things into YOUR_MODULE.views.inc for the sake of clarity, except if you have very little views stuff.
    – Kojo
    Aug 29, 2014 at 13:45
  • 1
    I don't believe Zoheir can change that from the GUI since the "Filters" and "Contextual Filters" are not in the same section. Last time I checked only the "Filters" have the OR/AND and Grouping ability. Aug 29, 2014 at 14:55
1

Download and install the module Views arguments in filters. It is a sandbox module so you will have to use git.

In your view, remove your contextual filter and replace it with a global null contextual filter.

enter image description here

Then add the filter that you want as the contextual filter to the normal filter area in views, and in its value argument set the value as %1 (ie you are ferencing the global null contextual filter).

enter image description here

Now rearrange the filter and-or groups to your liking.

enter image description here

1
  • @J.Reynolds This answer did get posted about 8.5 hrs after the edit to the first answers. However, I would say that these are different enough to warrant being standalone. Your answer includes details about how to use the sandbox module, the other just mentions it.
    – mpdonadio
    Aug 31, 2014 at 13:59

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.