How to condition SelectQuery by field value? I have a view that receives taxonomy terms. Via pre execute hook, I want to only get terms the terms that have the current user referenced (in their field). I know it works via views itself, but there is more to it. I have to do it programmatically.

function tk_access_views_pre_execute(&amp;$view) {
  if ('myview' == $view-&gt;name) {
    global $user;

    if (in_array('extern', $user-&gt;roles)) {
      $query = $view-&gt;build_info['query'];
      // debug($query-&gt;conditions());
      // $query-&gt;condition('field_referenced_user', $user-&gt;uid);
      $query-&gt;join(
        'field_data_field_referenced_user', 
        'referenced_user'
        // 'field_data_field_referenced_user.field_referenced_user_target_id = '
      );
      $query-&gt;condition('referenced_user.field_referenced_user_target_id', $user-&gt;uid);
      $view-&gt;build_info['query'] = $query;
    }
    // debug(array_keys($view-&gt;build_info));
  }
}


But it doesnt work, unfortunately. What am I doing wrong?

Edit

If I add a contextual filter inside the view, I can debug the final sql statement:

SELECT 
  taxonomy_term_data.name AS taxonomy_term_data_name,
  taxonomy_term_data.vid AS taxonomy_term_data_vid,
  taxonomy_term_data.tid AS tid,
  taxonomy_vocabulary.machine_name AS taxonomy_vocabulary_machine_name,
  'taxonomy_term' AS field_data_field_customer_taxonomy_term_entity_type,
  'myview:entityreference_1' AS view_name 

FROM 
  {taxonomy_term_data} taxonomy_term_data 
LEFT JOIN 
  {taxonomy_vocabulary} taxonomy_vocabulary ON taxonomy_term_data.vid = taxonomy_vocabulary.vid 
INNER JOIN 
  {field_data_field_projectstatus} field_data_field_projectstatus ON taxonomy_term_data.tid = field_data_field_projectstatus.entity_id 
    AND (field_data_field_projectstatus.entity_type = :views_join_condition_0 
    AND field_data_field_projectstatus.deleted = :views_join_condition_1) 
LEFT JOIN 
  {field_data_field_referenced_user} field_data_field_referenced_user ON taxonomy_term_data.tid = field_data_field_referenced_user.entity_id 
  AND (field_data_field_referenced_user.entity_type = :views_join_condition_2 
  AND field_data_field_referenced_user.deleted = :views_join_condition_3) 

WHERE 
  (((field_data_field_referenced_user.field_referenced_user_target_id = :field_data_field_referenced_user_field_referenced_user_target_id )
   )
  AND(( (taxonomy_vocabulary.machine_name IN (:db_condition_placeholder_4)) AND (field_data_field_projectstatus.field_projectstatus_value IN (:db_condition_placeholder_5, :db_condition_placeholder_6)) ))) 


ORDER BY taxonomy_term_data_name ASC


I still dont know how to get the additional conditions via SelectQuery object.
Yay, I dit it:

$query = $view-&gt;build_info['query'];
$query-&gt;leftJoin(
  'field_data_field_referenced_user', 
  'field_referenced_user',
  'taxonomy_term_data.tid = field_referenced_user.entity_id'
);
$query-&gt;where(
  'field_referenced_user.field_referenced_user_target_id = :uid',
  array(':uid' =&gt; $user-&gt;uid)
);

$view-&gt;build_info['query'] = $query;