1

Users have a "school" term reference field. When user visits his own user page, I want him to see his classmates listed.

For example, imagine that I have 3 students from the same school. When student A id:4 visits user/4, I want all student B and student C listed.

I can do this on term's page, term/10, but I could not figure out how to do it on user's page.

I believe it would have to be configured through contextual filter but I could not find a way yet.

Any help will be greatly appreciated!!

2 Answers 2

0
  1. Create a view block of users and show all the required fields.

  2. Add a contextual filter for school.

  3. Now here ideally we need some way to dynamically get the school id of the current logged in user. But we only get these kinds of values:enter image description here

  4. So in order to dynamically get the term id of the user's field. we can either write a custom hook hook_views_pre_view See its documentation and modify the correct $arg by loading the current user and getting the user's selected term for school. or

  5. Create a custom views argument plugin, or use this module : https://www.drupal.org/project/user_field_value

0

This is a common request to list sibling nodes in Views. You can search to find other examples.

In your case:

  1. Create Views listing all Users with Page or Block Display

  2. Add a Relationship Content referenced from field_school (or however it's called in your particular case), you can require it, this introduces the School into the Views.

  3. Add another Relationship Content using field_school, set it to use the Relationship from the previous step, you can require it. This is the reverse direction, pointing to all children that are pointing to this parent (School).

  4. Add the Contextual Filter User ID and configure it:
    Relationship field_school (the second one)
    When the filter value is NOT available
    Provide default value
    User ID from route context
    When the filter value IS available or a default is provided
    Specify validation criteria
    User ID

  5. If this is a Block Display place it on Child nodes and it will show other children that relate to the same Section. Don't forget to configure both the Block Visibility and Contextual settings.

    If you're using a Page Display change your Path setting to user/%user/[schoolmates] (change the brackets to what you like) and Menu to Menu tab "[Your tab name]". This will add a Drupal tab on the user profile pages that shows this Views, listing content that is related to the terms chosen by the user in their profile settings.

  6. To remove the current user from the list add another Contextual filter User ID and configure it with these options:
    When the filter value is NOT available
    Provide default value
    User ID from route context
    More
    Exlude

To check the Contextual filter with Views Preview, enter the ID of the Users you want to simulate, and enter it for each Contextual filter.

If you have both the contextual filter that identifies siblings and the one that excludes the current user from the list (and the Child ID is 24) enter 24/24.

If you want to see all the results, unaffected by Contextual filters enter all/all.

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.