I want to filter my content view by a field on the logged in user (the content type has that same field). I could implement hook_query_alter
and add add a where condition with the field from $global user
, but is there a way to do this out of the box though the UI?
2 Answers
There is no way to accomplish this via UI. At least not if the User field may contain multiple values.
In the example below 'field_test' is a List(text)
field w/ select widget (unlimited values allowed for user account). When the same field is added to Content type - I set it to allow for single values.
The code will take in consideration multiple values from User field and add WHERE
clause to the View that shows all nodes of a Content type.
WHERE
clause filters out all nodes that have 'field_test'
set to anything other than what currently logged in user has set in 'field_test'
on their own account.
/**
* Implements hook_views_query_alter().
*/
function MYMODULE_views_query_alter(&$view, &$query) {
// 'test_related_field' -> view's machine_name
if ($view->name == 'test_related_field') {
global $user;
$user = user_load($user->uid);
// 'field_test' -> field that is shared between user accounts ans content type
$user_field = field_get_items('user', $user, 'field_test');
if($user_field) {
$query->ensure_table("field_data_field_test");
// create query that processes multiple values of User 'field_test'
$db_or = db_or();
foreach ($user_field as $item){
$db_or->condition('field_data_field_test.field_test_value', $item, 'IN');
}
$query->add_where('test_field_group', $db_or);
}
// use dpm() to see added WHERE clause
//dpm($query);
}
}
UPDATE: If User field and Content Type field are allowed to have only single values then you can use UI for filtering view's results.
- add shared field as Contextual Filter
- Provide Default value for the field -> select PHP Code
- enter the following code in the box
global $user;
$user_fields = user_load($user->uid);
$test_field = $user_fields->field_test['und'][0]['value'];
return $test_field;
-
-
it works for single and multiple values. The code simply loads the field values and loops through values array. Then according to each value - adds appropriate WHERE clause. If there is only one value or the field allows for only one value, then only one WHERE clause will be added to the View query.– oksana-cFeb 18, 2016 at 21:41
-
Right. I was still wondering about the UI though. You have a qualifier: "There is no way to accomplish this via UI. At least not if the User field may contain multiple values." Feb 18, 2016 at 21:48
-
-
1Hmm. Thanks, but if I have to use php code at all I think I should put it in a custom module. Feb 18, 2016 at 22:07
Don't think there is a way. For the record, here's some starter code.
/**
* Implements hook_views_query_alter().
*/
function MYMODULE_views_query_alter(&$view, &$query) {
if ($view->name == VIEWNAME) {
global $user;
$user = user_load($user->uid);
$user_field = field_get_items('user', $user, 'field_NAMEOFFIELD');
if ($user_field) {
// only care about the first result for now
$user_field = $user_field[0]['value'];
}
$query->ensure_table("TABLENAME");
$query->add_where(0, "TABLENAME.NAMEOFFIELD_value", $user_field);
}
}
I want to filter my content view by a field on the logged in user
??? Not sure if I understand this correctly. So the logged in user has field x, which containts the wordhello
and you want to the filter the view by the wordhello
?