0

For example, consider a dating website. Each user has an Age stored in their profile. They also have a Max age and Min age, which are the maximum and minimum ages of people they hope to receive interest from (in other words, users with ages outside the range Min age - Max age should not be allowed to contact that user).

How can I build a page in Views that filters the results for the current user to show other users that both meet the current user's age range and for which the current user meets their age ranges?

In other words, I need to somehow filter the results like this:

  • Only show users that have ages within the range of the current user's Max age and Min age (example: current user is 20 and wants to see users from 18-25; filter to show users from 18-25)
  • Only show users for which the current user has an age that falls within the range of their Max age and Min age (example: current user is 20 and wants to see users from 18-25; filter to show users who want to receive messages from someone age 20)

Is this something I can somehow do with contextual filters, or do I need to write my own custom filter?

2
  • 1
    How about using Views Contextual Range Filter for the view contextual filter, then constructing the links to this page dynamically by loading current user and appending min/max onto link, or overriding a views hook and adding parameters there. Commented Dec 21, 2014 at 4:39
  • @J.Reynolds That's a great module and a good start, but I realized there are two separate filters here. Please see the updated question. Commented Dec 21, 2014 at 5:01

1 Answer 1

1

Install sandbox project Views arguments in filters

Add four contextual filters to your user view:

  1. User:Uid, hide view when filter not available, Exclude (you will send in the user's own uid and exclude him from the result as you do not want him to be matched against himself)

  2. Global:Null, hide view when filter not available, and give it an administrative title of 'current age'. (You will send it the current user's age as a param).

  3. Global:Null, hide view when filter not available, and give it an administrative title of 'min age'. (You will send it the current user's min age preference as a param).

  4. Global:Null, hide view when filter not available, and give it an administrative title of 'max age'. (You will send it the current user's max age preference as a param).

enter image description here

Then add normal filters as follows:

enter image description here

enter image description here

That should take care of the view, and you now just have to construct a link to add the parameters manually, or override a view hook to do so. Possible candidate would be hook_views_pre_view where you have direct access to args param, but on other hooks it should be available on the $view object.

Something like this code will work, and you can just add your own defensive programming and substitute where appropriate.

function MYMODULE_views_pre_view(&$view, &$display_id, &$args) {
  if ($view->name == "compatible" &&  $display_id == "page" && user_is_logged_in() && count($args) != 4) {
    global $user;
    $account  = user_load($user->uid);
    $age = field_get_items("user", $account, "field_age");
    $min_age = field_get_items("user", $account, "field_min_age");
    $max_age = field_get_items("user", $account, "field_max_age");
    $args = array(
      $account->uid,
      $age[0]['value'],
      $min_age[0]['value'],
      $max_age[0]['value']
    );
  }
}

Your Answer

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

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