Drupal Views Contextual Filters OR Logic

Drupal's eyes bug out when you try either/or logic with Views Contextual Filters.
Drupal's eyes bug out when you try either/or logic with Views Contextual Filters.

Drupal Views (standard) filters allow for AND/OR logic and groupings. However, when one wants to specify multiple CONTEXTUAL filters (previously referred to as arguments) you do NOT have contextual filters available. There is a "master thread" running on this issue located at https://drupal.org/node/1451218 for you to track news & progress on the issue.

In the meantime, I am having a bit of luck on most use cases by using a "Global PHP" filter in the regular filters section.

In this example, I wish to show a record if EITHER THE AUTHOR OR THE RECIPIENT FIELD equal the currently-logged-in user. My code in the "Filter code" area of the filter dialogue looks like this:

$me = $GLOBALS['user']->uid;
$send_uid = $row->uid;
$recip_uid = $row->uid_1;
 
if ($send_uid == $me || $recip_uid == $me) {
  return FALSE;
} else {
  return TRUE;
}

Note the use of "$row->uid_1" in the $recip_uid assignment - that is from a relationship I had to establish with the recipient User Reference field in the content type. Otherwise, the $row object that is available to the filter PHP does not have that information - it is nowhere near as robust as the $row object that is available to a field. Filters are very "challenged" in that sense. If you are having trouble implementing the equivalent of OR Logic for a contextual field by using PHP fields in the "regular" filtering area, be sure to know for certain what is being returned in your $row or $data objects. Unlike the huge objects that are available to PHP fields>, chances are the thing you are looking for in a filter's $row or $data isn't in there - unless you distinctly add it to the view.

Tags: