Hi there and thanks for your great module :-)

I want to display an info inside a block if there is a pending friend request.
For the privatemsg module I used this kind of snippet:

<?php
$num = privatemsg_unread_count();
if ($num > 0) {
  print $num ." new messages";
}
?>

Is there a silimar solution for the flag friend module?

Thanks for your answer,

Culfin

Comments

sirkitree’s picture

The simplest solution would be to create a block in Views. See the view that comes with the module and create a block display that has similar settings to the 'Pending' page display. I think the only thing you might want to change is where the argument handler gets it's default argument, which is the user - I think the page display's argument gets the user from the url, and you would want to get it from the currently logged in user.

liliplanet’s picture

Title: Display Pending friend requests inside a block? » Display pending friend requests as a menu item like messages with (x) amount

Hi,

If I may go a little further on this request.

As private messages show 'Messages (4)' as a menu item, in other words '4 waiting messages' ..

It would be stunning if Flag Requests could have for instance 'Friend Requests (5)' in the menu (my case secondary menu).

Is that at all possible please? Flag friend is naturally our most important networking module :)

Look forward to any reply, and thank you.
Lilian

sirkitree’s picture

I've actually done the same thing on our new website, but since our menu system is custom coded, I did not actually change the view title. I'm actually not sure how to dynamically change the view title to tell you the truth.

sirkitree’s picture

Version: 6.x-1.0-beta4 » 6.x-1.0-rc1
Component: User interface » Views integration
Status: Active » Closed (fixed)

Getting that count is a bit tricky as well. Please see #434020: Get total count of friends by $flag->get_user_count

Ralla’s picture

Here's how I did it:

global $user;
$friend_flag_fid = flag_get_flag('friend');
$pending_friends = db_result(db_query('SELECT COUNT(*) FROM {flag_content} WHERE fid = %d AND content_id = %d', $friend_flag_fid->fid, $user->uid));
print $pending_friends;
Caderial’s picture

Where would i put this code to implement it? and then how would i show the link in my secondary links menu?

Ralla’s picture

I haven't really done this, but this might work - I didnt test this though :) And there's probably a much better way to do this..

Put this in template.php for your theme. Remember to replace "yourtheme" with the actual name of your theme.

function _get_pending_friend_requests($uid) {
  $friend_flag_fid = flag_get_flag('friend');
  $pending_friends = db_result(db_query('SELECT COUNT(*) FROM {flag_content} WHERE fid = %d AND content_id = %d', $friend_flag_fid->fid, $uid));
  return $pending_friends;
}

function yourtheme_menu_item_link($link) {
  if ($link['link_path'] == 'the path of the menu item that should have the count added.') {
    global $user;
    $new_friend_requests = _get_pending_friend_requests($user->uid);
    $link['title'] = $link['title'] . ' ('. $new_friend_requests .')';
  }
}
garciac1212’s picture

#5 works great! the only prob is that I want it to display only if there IS a friend request/pending. If there isn't any I don't want it to display "0" like it does by default.

Any way around this?

sibany’s picture

hey!, garciac1212

whats wrong?

am still learning php.. :)

so her what i did :

in template.php

i past the code like this:

function _get_pending_friend_requests($uid) {
$friend_flag_fid = flag_get_flag('friend');
$pending_friends = db_result(db_query('SELECT COUNT(*) FROM {flag_content} WHERE fid = %d AND content_id = %d', $friend_flag_fid->fid, $uid));
return $pending_friends;
}

function tendu_menu_item_link($link) {
if ($link['user/[user-id]/friends/pending'] == 'the path of the menu item that should have the count added.') {
global $user;
$new_friend_requests = _get_pending_friend_requests($user->uid);
$link['title'] = $link['title'] . ' ('. $new_friend_requests .')';
}
}

by the way, i put the link menu friend request "the path of the menu item" as token to show for each user...
user/[user-id]/friends/pending
also tried this...
user/%/friends/pending

M.sibany

shakylegz’s picture

Hi..

Anyone that wants to have a block showing the number pending friend requests can use this code(Drupal 7):


global $user;
$friend_flag_fid = flag_get_flag('friend');
$fid = $friend_flag_fid->fid;
$content_id = $user->uid;

$q = "SELECT COUNT(*) FROM {flag_content} WHERE fid = :fid AND content_id = :content_id";
$result = db_query($q, array(':fid' => $fid, ':content_id' => $content_id)) ->fetchField();

print $result;

Regards

dankung1’s picture

Hi, thank you,hakylegz .

I modified a little, just add a URL to deal with the friend request.
tested on drupal 7


global $user;
$friend_flag_fid = flag_get_flag('friend');
$fid = $friend_flag_fid->fid;
$content_id = $user->uid;
$q = "SELECT COUNT(*) FROM {flag_content} WHERE fid = :fid AND content_id = :content_id";
$result = db_query($q, array(':fid' => $fid, ':content_id' => $content_id)) ->fetchField();
print "<a href=\"http://fab.chicpieces.com/user/" . $user->uid . "/friends/pending\">" . $result . "</a>";
sibany’s picture

God Bless you Guys!

Thanks! :)

sharmavijay86’s picture

HI i am
new to drupal
and using flag with flagfriend module.
tried above in a new block phpcode. i checked in mysql there is no table called {flag_content}.
I wanted to show message of new friend requests count to a user when he login.
Please help!

giorgiopg’s picture

For all those still interested. It worked for me.

global $status;
$status='4';
$content_id = $user->uid;
$q = "SELECT COUNT(*) FROM {flag_friend} WHERE status =:status AND entity_id = :content_id AND NOT uid =:content_id";
$result = db_query($q, array(':status' => $status, ':content_id' => $content_id)) ->fetchField();
print "<a href=\"http://yourdomain/user/" . $user->uid . "/friends/pending\">" . $result . "</a>";