1

Is it possible to show user's friends on a user's page (located user/%/friends) with Views by using only Flag module? So without the Flag Friend module or any other modules.

2 Answers 2

1

Step 1:

Use the Flag module to create a flag labeled "Friend" (or something like that). Here is such Flag definition (in export format) you could use for it (just import it in your own site):

$flags = array();
// Exported flag: "Friend".
$flags['friend'] = array (
  'entity_type' => 'user',
  'title' => 'Friend',
  'global' => '0',
  'types' => 
  array (
  ),
  'flag_short' => 'Friend',
  'flag_long' => 'Mark as friend',
  'flag_message' => 'User marked as friend',
  'unflag_short' => 'Unfriend',
  'unflag_long' => 'Remove as friend',
  'unflag_message' => 'User removed as friend',
  'unflag_denied_text' => '',
  'link_type' => 'toggle',
  'weight' => 0,
  'show_in_links' => 
  array (
    'full' => 'full',
  ),
  'show_as_field' => 0,
  'show_on_form' => 0,
  'access_author' => '',
  'show_contextual_link' => 0,
  'show_on_profile' => 1,
  'access_uid' => 'others',
  'api_version' => 3,
);
return $flags;

Step 2:

Use the Views module to create a view of users that flagged other users with that "Friend" flag. Here is such view (in export format) you could use for it (just import it in your own site):

$view = new view();
$view->name = 'my_friends';
$view->description = '';
$view->tag = 'default';
$view->base_table = 'users';
$view->human_name = 'My friends';
$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'] = 'Friends';
$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: Flags: friend */
$handler->display->display_options['relationships']['flag_content_rel']['id'] = 'flag_content_rel';
$handler->display->display_options['relationships']['flag_content_rel']['table'] = 'users';
$handler->display->display_options['relationships']['flag_content_rel']['field'] = 'flag_content_rel';
$handler->display->display_options['relationships']['flag_content_rel']['flag'] = 'friend';
/* 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: Last login */
$handler->display->display_options['fields']['login']['id'] = 'login';
$handler->display->display_options['fields']['login']['table'] = 'users';
$handler->display->display_options['fields']['login']['field'] = 'login';
$handler->display->display_options['fields']['login']['date_format'] = 'long';
$handler->display->display_options['fields']['login']['second_date_format'] = 'long';
/* Field: User: Uid */
$handler->display->display_options['fields']['uid']['id'] = 'uid';
$handler->display->display_options['fields']['uid']['table'] = 'users';
$handler->display->display_options['fields']['uid']['field'] = 'uid';
/* Field: Flags: Flagged time */
$handler->display->display_options['fields']['timestamp']['id'] = 'timestamp';
$handler->display->display_options['fields']['timestamp']['table'] = 'flagging';
$handler->display->display_options['fields']['timestamp']['field'] = 'timestamp';
$handler->display->display_options['fields']['timestamp']['relationship'] = 'flag_content_rel';
$handler->display->display_options['fields']['timestamp']['date_format'] = 'long';
$handler->display->display_options['fields']['timestamp']['second_date_format'] = 'long';
/* 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';
/* 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'] = 'user/%/friends';
$handler->display->display_options['menu']['type'] = 'tab';
$handler->display->display_options['menu']['title'] = 'Friends';
$handler->display->display_options['menu']['description'] = 'Users marked as friends';
$handler->display->display_options['menu']['weight'] = '9';
$handler->display->display_options['menu']['name'] = 'user-menu';
$handler->display->display_options['menu']['context'] = 0;
$handler->display->display_options['menu']['context_only_inline'] = 0;

Result

After you enable the view, the result of it is that it'll show a tabular list of users, via path user/%/friends, and it will add a "Friends" tab on the user profile page (also).

Here is a sample of such table:

Name           ! Last login                  ! Uid
---------------+-----------------------------+----
Dries.Buytaert ! Monday, May 2, 2016 - 18:19 ! 19
Demo.User      ! Friday, May 6, 2016 - 12:16 ! 8
7
  • I would like to precise that this view must be seen by other users. May 9, 2016 at 19:05
  • I suppose that Flags: friends (by any user) must be used instead of Flags: friends (by current user) in this case? May 9, 2016 at 19:08
  • To "see this view by other users", that should work too ... All that's needed would be to replace the "x" in the path like "user/x/friends" to the "uid" of the user for which you want to see that user's friend. Or am I missing something? PS: correct me if I'm wrong, but I think your 2nd comment is not correct (or otherwise I don't understand it). Let me know, ok? May 9, 2016 at 19:10
  • I mean that other users would be able to see another user's friends. For this view a user can only see his/her own friends. May 9, 2016 at 19:12
  • @gladiatorhl2: I'm still not sure I understand what you would "like" to happen ... If I'm "demo.user" (as in my "result"), I could use a path like user/19/friends (to see the friends from user "Dries.Buytaert"). Agree? If so: is that what you would "want", or do you NOT want to "allow" that? May 9, 2016 at 19:22
0

You can add a contextual filter to the User uid on the flag.

1
  • Could you indicate what this action is needed for? May 9, 2016 at 19:08

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.