Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drupal 7 Views contextual filters OR logic

Tags:

views

drupal

Contextual filters are applied in AND logic. Is there any way to have two or more contextual filters in OR logic?

like image 214
Nikos Tsagkas Avatar asked Nov 23 '12 15:11

Nikos Tsagkas


People also ask

What is contextual filter in Drupal view?

Contextual filters in Drupal allow you to add or remove information (content) to the currently displayed view. The filter acts dynamically based on the URL. If the provided argument value is in the URL, the view will contain certain results, if the value is not in the URL, the view will be filtered with other results.

What is the difference between contextual and normal filter on view layer?

A regular filter could give you all nodes written by a specified user. A contextual filter for a node author would be able to display all nodes written by the currently viewed user, or the same user who wrote the currently viewed node.

What is contextual filters model?

The title “contextual filters model” was chosen to convey that teachers' disciplinary views and related assumptions are stable antecedents to course planning, largely independent of context.


1 Answers

In views 7.x-3.5 this may not be possible using the UI.

Assume your module name is my_module

Add the following to your my_module.module file

<?php
function my_moudle_views_api() {
  return array(
    'api' => 3,
  );
}
?>

and

the following to my_module.views.inc

<?php
function eb_mine_views_query_alter(&$view, &$query) {
  if ($view->name == 'statuser') {
    dsm($query, 'before');
    $query->where[0]['type'] = 'OR';
    dsm($query, 'after');
  }
}
?>

Source : http://drupal.org/node/1451218#comment-6136692

Whereas in the earlier version of views, the AND/OR option of the contextual filter would be decided by that of the first group of static filters in the view.

Grouping of contextual filters
Even though contextual filters do not appear in the "and/or" user interface for sorting and grouping regular filters, contextual filters are always added to the first group of filters. Thus the order of the groups can cause the contextual filter to have entirely different effects on the results of a view that has contextual filters. Even though differences might not be apparent through the user interface.

Multiple contextual filters are therefore always in the same "and/or" group of filters, and can not be placed in different groups. There is an effort to add this feature.
like image 143
Gokul N K Avatar answered Sep 21 '22 15:09

Gokul N K