2

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?

5
  • 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 word hello and you want to the filter the view by the word hello?
    – No Sssweat
    Feb 18, 2016 at 1:39
  • @NoSssweat Edited question. Feb 18, 2016 at 1:48
  • I still don't get it.
    – No Sssweat
    Feb 18, 2016 at 1:59
  • The general problem is getting to fields attached to User (specifically the logged-in user) from a content view. Does this make sense? In my specific case, I have a view that shows pieces of content of a certain content type. On that content type there is a certain field. That same field is also attached to Users. I want to filter the view to only those content pieces that have the same setting as the current user. Feb 18, 2016 at 2:08
  • ok I get it now. humm yea I don't think there is a way with just the UI.
    – No Sssweat
    Feb 18, 2016 at 2:31

2 Answers 2

3

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;

6
  • Say the field doesn't allow multiples? Feb 18, 2016 at 21:06
  • 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-c
    Feb 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
  • right, check the updated answer for UI method
    – oksana-c
    Feb 18, 2016 at 22:03
  • 1
    Hmm. 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
2

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);
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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