Views: apply filter only if a condition matches I've searched a lot the Views issue queue, searched for other modules, searched on stackexchange, but it seems that no one needs this functionality (or that there's another way to achieve it).

Use case:
I have two content types: lecture and substitute teaching. A teacher uses the lecture content type when he has a normal lecture in a class where he has a chair. A teacher uses the substitute teaching when another teacher isn't at school (ill or in an excursion) and he has to replace the colleague.

In the lecture content type I have put an entity reference field named "chair", which filters the chairs so only the tearcher's ones are shown. For doing this I use the entity selection section of the field, and pass via token the teacher's uid to the view. But in the substitute teaching content type, I don't want to filter the chairs. Since a teacher could replace anyone, I need an unfiltered list of chairs.

Since the entity selection part of the field is applied everywhere this field is used, I had to create another entity reference field in the substitute teaching which references the chair content type, but without filtering. Having the same content type pointed, but using two different fields, is leading to maintenance nightmares (put both the content types and filter them based on chair field is nearly impossible), so I'm seeking for a different approach.

I think that a good one would be to pass to the view the uid and the content type. If the content type is "lecture", then apply filtering, otherwise don't. In this way I could use the same field in both the content types and everythig would work smothly. Well, it seems impossible to do such a simple thing.

The only solution that got me near the desired result was using conditional fields, but when filtering I got a bunch of empty option values where the view result was hidden, but it was selectable.

Any help or hint would be appreciated. Thanks!
You will have to add a custom module for this. I usually have one monolithic module that I add to every project that I name the same as my project for project specific tweaks like this.

In that module's module file, add this code:

function MODULE_views_query_alter(&amp;$view, &amp;$query){
  // target the correct view
  if ($view-&gt;name == 'chair' &amp;&amp; $view-&gt;current_display == 'entityreference_1') {
    // target the correct path
    $node = menu_get_object();
    if ($node &amp;&amp; arg(0) == 'node' &amp;&amp; arg(2) == 'edit' &amp;&amp; $node-&gt;type == 'lecture') {
      // edit page on lecture type
      $view-&gt;query-&gt;add_where("entityreference_tweak", "node.uid", $node-&gt;uid, '=');
    }
    else if (arg(0) == 'node' &amp;&amp; arg(1) == 'add' &amp;&amp; arg(2) == 'lecture')    
    // create page on lecture type
    {
      global $user;
      $view-&gt;query-&gt;add_where("entityreference_tweak", "node.uid", $user-&gt;uid, '=');
    }
  }
}


I named the entityreference view 'chair', the entityreference display took the default name 'entityreference_1', the lecture content type has the machine name 'lecture'. You need to substitute these name in the code above with your own machine names.

Then completely remove the contextual uid filter from your entity reference view and also do not pass anything to the view in the field settings of the content type. The code adds the filter dynamically. It checks if you are on a lecture edit/add page, then adds either the node author uid, or the uid of the currently logged in user.