Complex View Contextual Filter with User Relationships — how would I achieve this?
Fixed it.

Last night I noticed that I have the ability to add PHP Filter Criteria (which I'd somehow not noticed before!), coming from the Views PHP Module. It turns out that this allows me to enter arbitrary PHP and it will take anything that returns TRUE as meaning the row should be removed from the resultset.

So I added a Filter Criterion that checks the SQL query Select * From user_relationships Where requester_id = :current_uid And requestee_id = :follower_uid and I return TRUE; if that query results in more than zero rows.

To do this, I needed to add new Fields, both set to be "Excluded from display":


User: User ID (bound to Requester), with an administrative title of "Follower UID (not displayed)". This gets passed as the variable $row-&gt;uid
User: User ID (bound to Requestee), with an administrative title of "Current UID (not displayed)". This gets passed as the variable $row-&gt;uid_1


Then in a new PHP Filter Criterion, I created a filter as follows:

$filter_result = db_query('Select * From user_relationships '.
  'Where requester_id = :current_uid And requestee_id = :follower_uid',
    array(
        ':current_uid' =&gt; $row-&gt;uid_1,
        ':follower_uid' =&gt; $row-&gt;uid,
    ));

$count = $filter_result-&gt;rowCount();

return $count &gt; 0;


Then those superfluous rows no longer appear in my output.

For some reason, I do need to assign $filter_result-&gt;rowCount() to the variable $count; trying return $filter_result-&gt;rowCount() &gt; 0; directly didn't want to work for some reason.

But problem solved. At last  :o)



Edit: I've just corrected the Field bindings, having noticed I was doing it differently in two different places and  I'd put the wrong one in my answer here. Note that the User Relationships fields "UR: Requester user ID" and "UR: Requestee user ID" do not output user IDs, so do not work in this query; you do have to use two different "User: User ID" Fields, one bound to each end of the relationship. (Irritatingly, this means they have the really unhelpful variable names of $row-&gt;uid and $row-&gt;uid_1 when passed to the PHP Field; I haven't yet found a way around this.)