0
  • I created a "Group" vocabulary with the terms "Company 1", "Company 2", ...
  • I created a field on the account setting.
  • I linked my user account "Company 1" and 4 other users.
  • I created a view that displays a list of users.

How to display only users associated with the same duration as the currently logged in user?

thank you in advance

enter image description here

enter image description here

$view = new view();
$view->name = 'views_groupe';
$view->description = '';
$view->tag = 'default';
$view->base_table = 'users';
$view->human_name = 'Groupe';
$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'] = 'Groupe';
$handler->display->display_options['use_more_always'] = FALSE;
$handler->display->display_options['access']['type'] = 'none';
$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'] = 'none';
$handler->display->display_options['style_plugin'] = 'table';
$handler->display->display_options['style_options']['columns'] = array(
  'uid' => 'uid',
  'field_code_groupe' => 'field_code_groupe',
  'field_groupe' => 'field_groupe',
  'name' => 'name',
  'mail' => 'mail',
  'ops' => 'ops',
);
$handler->display->display_options['style_options']['default'] = '-1';
$handler->display->display_options['style_options']['info'] = array(
  'uid' => array(
    'sortable' => 0,
    'default_sort_order' => 'asc',
    'align' => '',
    'separator' => '',
    'empty_column' => 0,
  ),
  'field_code_groupe' => array(
    'sortable' => 0,
    'default_sort_order' => 'asc',
    'align' => '',
    'separator' => '',
    'empty_column' => 0,
  ),
  'field_groupe' => array(
    'sortable' => 0,
    'default_sort_order' => 'asc',
    'align' => '',
    'separator' => '',
    'empty_column' => 0,
  ),
  'name' => array(
    'sortable' => 0,
    'default_sort_order' => 'asc',
    'align' => '',
    'separator' => '',
    'empty_column' => 0,
  ),
  'mail' => array(
    'sortable' => 0,
    'default_sort_order' => 'asc',
    'align' => '',
    'separator' => '',
    'empty_column' => 0,
  ),
  'ops' => array(
    'align' => '',
    'separator' => '',
    'empty_column' => 0,
  ),
);
/* Field: User: Groupe */
$handler->display->display_options['fields']['field_groupe']['id'] = 'field_groupe';
$handler->display->display_options['fields']['field_groupe']['table'] = 'field_data_field_groupe';
$handler->display->display_options['fields']['field_groupe']['field'] = 'field_groupe';
$handler->display->display_options['fields']['field_groupe']['type'] = 'taxonomy_term_reference_plain';
/* 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']['label'] = 'Nom d\'utilisateur';
$handler->display->display_options['fields']['name']['alter']['word_boundary'] = FALSE;
$handler->display->display_options['fields']['name']['alter']['ellipsis'] = FALSE;
$handler->display->display_options['fields']['name']['link_to_user'] = FALSE;
/* 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';
/* Contextual filter: User: Groupe (field_groupe) */
$handler->display->display_options['arguments']['field_groupe_tid']['id'] = 'field_groupe_tid';
$handler->display->display_options['arguments']['field_groupe_tid']['table'] = 'field_data_field_groupe';
$handler->display->display_options['arguments']['field_groupe_tid']['field'] = 'field_groupe_tid';
$handler->display->display_options['arguments']['field_groupe_tid']['default_action'] = 'default';
$handler->display->display_options['arguments']['field_groupe_tid']['exception']['title'] = 'Tout';
$handler->display->display_options['arguments']['field_groupe_tid']['default_argument_type'] = 'current_user';
$handler->display->display_options['arguments']['field_groupe_tid']['summary']['number_of_records'] = '0';
$handler->display->display_options['arguments']['field_groupe_tid']['summary']['format'] = 'default_summary';
$handler->display->display_options['arguments']['field_groupe_tid']['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;

/* Display: Page */
$handler = $view->new_display('page', 'Page', 'page');
$handler->display->display_options['path'] = 'groupe';
$translatables['views_groupe'] = array(
  t('Master'),
  t('Groupe'),
  t('more'),
  t('Apply'),
  t('Reset'),
  t('Sort by'),
  t('Asc'),
  t('Desc'),
  t('Nom d\'utilisateur'),
  t('Tout'),
  t('Page'),
);
0

Views query alter should help.

/**
 * Implements hook_views_query_alter().
 */
function hook_views_query_alter(&$view, &$query) {
  if ($view->name == "views_groupe") { 
  global $user; 
  $user_data = user_load($user->uid);
  if (isset($user_data->field_groupe)) {
    $company_name = $user_data->field_groupe['und']['0']['tid'];
    $query->add_table('field_data_field_groupe');
    $query->add_where(0, 'field_data_field_groupe.field_groupe_tid', $company_name, '=');
  } 
 }
}

Remove all the contextual filters set, only have the filter you set under filter criteria.

| improve this answer | |
  • Your method works for nodes associated with the terms, but not for user related terms. This shows no result. Even putting a fixed value, it displays nothing. – user54067 Sep 21 '16 at 8:32
  • Hey Mathieu, I gave a try as well, but I'm able to pull the values.I created a view of the type user and had the company_field created in the user entity as a base field. Simply gave User:company_name in the contextual filters with the above criteria and was able to pull the result, let me know are we on the same page – user60996 Sep 21 '16 at 10:23
  • I've updated the question, but it still does not work. – user54067 Sep 21 '16 at 11:47
  • -Ok, I found the problem with the previous solution as my term id and user id were same and that they populated the result. I have given a simple query alter which should serve the purpose, Hope it helps :). – user60996 Sep 21 '16 at 15:00
  • thank you, you have to put it in the template.php my theme? – user54067 Sep 21 '16 at 15:33
0

You should be able to do this using contextual filters and relations.

  1. Add the contextual filter "User: uid".
  2. Under "WHEN THE FILTER VALUE IS NOT IN THE URL" select "Provide default value" -> "User id from logged in user".
  3. Add a required relationship: "Taxonomyterm: Content with term."
  4. Finally, on top of your view field preferences select your added relationship.
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy