I have a view that lists all user profiles. At registration, every user selects, from a 'sector' taxonomy, the sector they themselves do business in. They also select from the same 'sector' taxonomy to record what sectors they want to do business with. So their user profiles may look like this:

uid: 1
name: fred
sector in: arts
sectors to do business with: tech

uid: 2
name: alex
sector in: tech
sectors to do business with: design

I want a view to list all the users that do business in the sectors that the current user wants to do business in.

So, using the above profiles, if Fred was looking at the view, Alex's profile would be displayed because the filter would be:

sector == tech

Obviously, the filter needs to be dynamic. It needs to grab the 'sectors to do business with' field from Fred's user profile. I think I need a contextual filter, but how do I configure it?


1 Answer 1


Provided you have two fields on your user profile called field_working_in and field_want_to_work_with execute the following steps:

  • Add a Users view at admin/structure/views/add and set Display Format as Table
  • Add two fields User: Name and User: Working in
  • Add relationship with User: Working in
  • Add relationship with Taxonomy term: User using Want to work with
  • Add a contextual filter: User: uid with Provide Default Value and Type: User ID form logged in User and set a relationship with field_want_to_work_with (see screenshot)
  • Under FILTER CRITERIA Add the following filter to exclude the current user from the results: User: Current (No)

Export of view:

$view = new view();
$view->name = 'user_sections';
$view->description = '';
$view->tag = 'default';
$view->base_table = 'users';
$view->human_name = 'User sections';
$view->core = 7;
$view->api_version = '3.0';
$view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */

/* Display: Master */
$handler = $view->new_display('default', 'Master', 'default');
$handler->display->display_options['title'] = 'User sections';
$handler->display->display_options['use_more_always'] = FALSE;
$handler->display->display_options['access']['type'] = 'perm';
$handler->display->display_options['access']['perm'] = 'access user profiles';
$handler->display->display_options['cache']['type'] = 'none';
$handler->display->display_options['query']['type'] = 'views_query';
$handler->display->display_options['exposed_form']['type'] = 'basic';
$handler->display->display_options['pager']['type'] = 'full';
$handler->display->display_options['pager']['options']['items_per_page'] = '10';
$handler->display->display_options['style_plugin'] = 'table';
/* Relationship: User: Working in (field_working_in) */
$handler->display->display_options['relationships']['field_working_in_tid']['id'] = 'field_working_in_tid';
$handler->display->display_options['relationships']['field_working_in_tid']['table'] = 'field_data_field_working_in';
$handler->display->display_options['relationships']['field_working_in_tid']['field'] = 'field_working_in_tid';
/* Relationship: Taxonomy term: User using Want to work with */
$handler->display->display_options['relationships']['reverse_field_want_to_work_with_user']['id'] = 'reverse_field_want_to_work_with_user';
$handler->display->display_options['relationships']['reverse_field_want_to_work_with_user']['table'] = 'taxonomy_term_data';
$handler->display->display_options['relationships']['reverse_field_want_to_work_with_user']['field'] = 'reverse_field_want_to_work_with_user';
$handler->display->display_options['relationships']['reverse_field_want_to_work_with_user']['relationship'] = 'field_working_in_tid';
/* Field: User: Name */
$handler->display->display_options['fields']['name']['id'] = 'name';
$handler->display->display_options['fields']['name']['table'] = 'users';
$handler->display->display_options['fields']['name']['field'] = 'name';
$handler->display->display_options['fields']['name']['alter']['word_boundary'] = FALSE;
$handler->display->display_options['fields']['name']['alter']['ellipsis'] = FALSE;
/* Field: User: Working in */
$handler->display->display_options['fields']['field_working_in']['id'] = 'field_working_in';
$handler->display->display_options['fields']['field_working_in']['table'] = 'field_data_field_working_in';
$handler->display->display_options['fields']['field_working_in']['field'] = 'field_working_in';
$handler->display->display_options['fields']['field_working_in']['label'] = '';
$handler->display->display_options['fields']['field_working_in']['element_type'] = '0';
$handler->display->display_options['fields']['field_working_in']['element_label_colon'] = FALSE;
$handler->display->display_options['fields']['field_working_in']['element_wrapper_type'] = '0';
$handler->display->display_options['fields']['field_working_in']['element_default_classes'] = FALSE;
$handler->display->display_options['fields']['field_working_in']['delta_offset'] = '0';
$handler->display->display_options['fields']['field_working_in']['field_api_classes'] = TRUE;
/* Sort criterion: User: Created date */
$handler->display->display_options['sorts']['created']['id'] = 'created';
$handler->display->display_options['sorts']['created']['table'] = 'users';
$handler->display->display_options['sorts']['created']['field'] = 'created';
$handler->display->display_options['sorts']['created']['order'] = 'DESC';
/* Contextual filter: User: Uid */
$handler->display->display_options['arguments']['uid']['id'] = 'uid';
$handler->display->display_options['arguments']['uid']['table'] = 'users';
$handler->display->display_options['arguments']['uid']['field'] = 'uid';
$handler->display->display_options['arguments']['uid']['relationship'] = 'reverse_field_want_to_work_with_user';
$handler->display->display_options['arguments']['uid']['default_action'] = 'default';
$handler->display->display_options['arguments']['uid']['default_argument_type'] = 'current_user';
$handler->display->display_options['arguments']['uid']['summary']['number_of_records'] = '0';
$handler->display->display_options['arguments']['uid']['summary']['format'] = 'default_summary';
$handler->display->display_options['arguments']['uid']['summary_options']['items_per_page'] = '25';
/* Filter criterion: User: Active */
$handler->display->display_options['filters']['status']['id'] = 'status';
$handler->display->display_options['filters']['status']['table'] = 'users';
$handler->display->display_options['filters']['status']['field'] = 'status';
$handler->display->display_options['filters']['status']['value'] = '1';
$handler->display->display_options['filters']['status']['group'] = 1;
$handler->display->display_options['filters']['status']['expose']['operator'] = FALSE;
/* Filter criterion: User: Current */
$handler->display->display_options['filters']['uid_current']['id'] = 'uid_current';
$handler->display->display_options['filters']['uid_current']['table'] = 'users';
$handler->display->display_options['filters']['uid_current']['field'] = 'uid_current';
$handler->display->display_options['filters']['uid_current']['value'] = '0';

/* Display: Page */
$handler = $view->new_display('page', 'Page', 'page');
$handler->display->display_options['path'] = 'user-sections';
$handler->display->display_options['menu']['type'] = 'normal';
$handler->display->display_options['menu']['title'] = 'User sections';
$handler->display->display_options['menu']['weight'] = '0';
$handler->display->display_options['menu']['name'] = 'main-menu';
$handler->display->display_options['menu']['context'] = 0;
$handler->display->display_options['menu']['context_only_inline'] = 0;
$translatables['user_sections'] = array(
  t('User sections'),
  t('Sort by'),
  t('Items per page'),
  t('- All -'),
  t('« first'),
  t('‹ previous'),
  t('next ›'),
  t('last »'),
  t('term from field_working_in'),

enter image description here

  • On bullet point 5, what do you mean by 'and set a relationship with field_want_to_work_with'?
    – dbj44
    Commented Jan 20, 2017 at 12:04
  • Updated with a screenshot to clarify
    – devunder
    Commented Jan 20, 2017 at 12:08
  • hmm ok just saw that you added the tag that this is for drupal 8. not sure if this will work in that version :)
    – devunder
    Commented Jan 20, 2017 at 12:12
  • Yea, I saw version 7 in your export and thought I'd better add a tag. I don't have the 'Relationship' select list that you have circled?
    – dbj44
    Commented Jan 20, 2017 at 12:15
  • Just trying in d8 now and I did got the relationship field but it seems only when I first entered the contextual filter and then the relationships. right now fiddling around with the relationships but there is some weird stuff going around with Unknown column 'entity_id' errors which is causing me to not saving the view after some changed.
    – devunder
    Commented Jan 20, 2017 at 12:50

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.