Title pretty much explains it. I have a user entity reference field (Node) called "Company". I want to show all content in a view where the author of that content has the same "company" value as the logged in user. I'm getting a little confused with the relationships to achieve this.
Ok, I was able to get this working based on Kien's answer. If using Entity Reference 7-x.1.5 Instead of 'field_company_nid', the correct column name was 'field_company_target_id'
/**
* Implements hook_views_query_alter().
*/
function your_module_views_query_alter(&$view, &$query) {
if ($view->name == 'article' && $view->current_display == 'page') { // replace 'article' and 'page' by your view's machine name and display's machine name
$author_join = new views_join();
$author_join->table = 'field_data_field_company'; // 'field_company' is machine name of the user's field referencing to Company
$author_join->field = 'entity_id';
$author_join->left_table = 'node';
$author_join->left_field = 'uid';
$author_join->type = 'left';
$view->query->add_relationship('author_company', $author_join, 'node');
$current_join = new views_join();
$current_join->table = 'field_data_field_company'; // 'field_company' is machine name of the user's field referencing to Company
$current_join->field = 'field_company_target_id';
$current_join->left_table = 'author_company';
$current_join->left_field = 'field_company_target_id';
$current_join->type = 'left';
$view->query->add_relationship('current_company', $current_join, 'author_company');
global $user;
$view->query->where[1]['conditions'][] = array(
'field' => 'current_company.entity_id',
'value' => $user->uid,
'operator' => '='
);
}
}
What you want seems a bit difficult to do by simply setting the view. You might need some custom code to alter the view query. Below is an approach using hook_views_query_alter():
hook_views_query_alter()
:/**
* Implements hook_views_query_alter().
*/
function your_module_views_query_alter(&$view, &$query) {
if ($view->name == 'article' && $view->current_display == 'page') { // replace 'article' and 'page' by your view's machine name and display's machine name
$author_join = new views_join();
$author_join->table = 'field_data_field_company'; // 'field_company' is machine name of the user's field referencing to Company
$author_join->field = 'entity_id';
$author_join->left_table = 'node';
$author_join->left_field = 'uid';
$author_join->type = 'left';
$view->query->add_relationship('author_company', $author_join, 'node');
$current_join = new views_join();
$current_join->table = 'field_data_field_company'; // 'field_company' is machine name of the user's field referencing to Company
$current_join->field = 'field_company_nid';
$current_join->left_table = 'author_company';
$current_join->left_field = 'field_company_nid';
$current_join->type = 'left';
$view->query->add_relationship('current_company', $current_join, 'author_company');
global $user;
$view->query->where[1]['conditions'][] = array(
'field' => 'current_company.entity_id',
'value' => $user->uid,
'operator' => '='
);
}
}