2

Somehow I am not getting it.

I have created a Taxonomy of Organizations. I have then added a custom required field to my User accounts call "Organization". I would like to create a view where the currently logged in user can only see a list of people that belongs to the same Organization as the currently logged in user and no others.

I have created the view just fine, and the list of users appears fine, but I cannot get the filtering and relationships to only show me the only the users that belong to the same Organization as the currently logged in user.

I could do this in SQL in like 2 seconds, but for some reason I am not getting how to do it in the Drupal interface.

I am running Drupal 8.

Kien Nguyen
  • 2,616
  • 2
  • 7
  • 24
karozans
  • 53
  • 7

1 Answers1

2
  1. Add a contextual filter User ID (to get the current user) enter image description here enter image description here

  2. Add a relationship Taxonomy term referenced from field_organization (to get the taxonomy term referenced by current user) enter image description here

  3. Add a relationship User using field_organization (to get the users referencing to the Organization taxonomy at step 2) enter image description here

  4. Change the relationship of all fields to field_organization enter image description here

  5. If you want to exclude the current user from the results, add another contextual filter User ID with field_organization relationship enter image description here Remember to scroll down and check the Exclude enter image description here

Kien Nguyen
  • 2,616
  • 2
  • 7
  • 24
  • Thanks for the help. This works. However, I am not sure what is really going on here so I am having difficulty figuring things out. Now I want to extend it. Each user has an organization, and a parent organization field. Both fields use the same taxonomy. I want the currently logged in user to see a list of users that have the same organization and parent organization as the currently logged in users organization. Does that makes sense? – karozans May 24 '21 at 09:33
  • Here is a SQL statement that can help demonstrate what I want. I know this is not a valid SQL statement for drupal, it is just for illustration. SELECT * FROM users WHERE current_logged_in_user.organization = users.organization OR current_logged_in_user.organization = users.parent_organization; – karozans May 24 '21 at 10:24
  • @karozans I understand. But I think it can't be done through Drupal view interface. You can implement some custom code to alter the view query. – Kien Nguyen May 25 '21 at 05:04
  • 1
    Gotcha. Thanks for the help. Actually I think I can get it to work by linking the current user organization to the parent organization of the user they are creating and it seems to do what I want it to do. – karozans May 26 '21 at 19:07