1

Firstly in my site, using Profile 2. i created a 'Select list field' that allows each user to choose his/her country.

I then created a custom user profile page using views by following these steps:

1.Create a view and add a "page" display. 2. Use "user/%" as the page path. 3. Add a contextual filter "user uid" and select "provide default value" user ID from URL.

Now i want to add a block to each users profile page that displays all users from the same country as the currently viewed user profile

I have tried adding the "Profile Country" contextual filter and selecting "provide default value" user ID from URL but no results are displayed.

Is there a way to do this?

2 Answers 2

2

The problem is you're relying on your contextual filter to mean 2 things:

  • For the page it has to be the UID.
  • For the block it has to be the country select value that corresponds to the UID.

If you're wanting to configure it this way, you'll need your block view to derive the profile country value from a UID. One way you could do this is to use Provide default value > Type: PHP Code options with something like this:

if ($user = user_load(arg(1))) {
  $profile = profile2_load_by_user($user);
  return $profile['my_profile_machine_name']->field_my_country_field_name[LANGUAGE_NONE][0]['value'];
}

The actual code will vary based on your Profile2 configuration.

2
  • Thanks! It worked. One more thing, would you know how i can exclude the currently viewed profile from the block view? (P.S: would you prefer i post this as a separate question?)
    – seyi
    Jul 17, 2016 at 4:14
  • 2
    Add another contextual filter; you can add the User: Uid filter as you previously did, but use the More > Exclude option to negate the effects.
    – Shawn Conn
    Jul 17, 2016 at 16:47
0

Try the following:

  • Create a new view that shows Profiles of the types you defined.
  • Select "Create block" instead of "Create page".
  • Give it a title, and select a display, either table, unformatted list of fields or entities, or anything else
  • Add a "Contextual filter". Select the Country field as contextual filter
  • In "When filter value is not available" choose "Provide default value"
  • Select Type: PHP code

Use a variation of this code, without the php tag

global $user;
$profile = profile2_by_uid_load($user->uid, 'YOUR_PROFILE_MACHINE_NAME');
$filter = $profile->field_YOUR_COUNTRY_FIELD_NAME[LANGUAGE_NONE][0]['value'];
return $filter;

So, what this code does is: it provides a value to the contextual filter for your profile view, so that only records that match the country code for the currently logged in user will be shown.

NB: you may need to modify the part after [LANGUAGE_NONE] depending on how the value for your country field is store. Alternatively you can use a entity_metadata_wrapper in line 3, as in

$w = entity_metadata_wrapper('profile2', $profile); 
$filter = $w->field_YOUR_COUNTRY_FIELD_NAME->value();

Additionally you may want to test if the field for Country is empty before everything else.

Don't forget to enable the block in Structure > Blocks so that you can view it.

Good luck.

2
  • There are a few differences between my example and @Shawn Conn's. The main one is that I user global $user to refer to the currently logged in user. His uses the uid provided in the URL. Re-reading your post I now realize that you want to show same country for the CURRENTLY VIEWED profile. Then his code is the one to use.
    – argiepiano
    Jul 16, 2016 at 21:56
  • Many thanks for this. Yes the code provided by @shawn solves the issue.
    – seyi
    Jul 17, 2016 at 4:09

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.