0

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(&$view) {
  if ('myview' == $view->name) {
    global $user;

    if (in_array('extern', $user->roles)) {
      $query = $view->build_info['query'];
      // debug($query->conditions());
      // $query->condition('field_referenced_user', $user->uid);
      $query->join(
        'field_data_field_referenced_user', 
        'referenced_user'
        // 'field_data_field_referenced_user.field_referenced_user_target_id = '
      );
      $query->condition('referenced_user.field_referenced_user_target_id', $user->uid);
      $view->build_info['query'] = $query;
    }
    // debug(array_keys($view->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.

0

Yay, I dit it:

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

$view->build_info['query'] = $query;
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.