Hi,

Thank you for your wonderful module.

I was hoping for a simple block with 'you have x relationships request pending' with a link to pending requests.

As the message only shows when you login, would prefer something more persistent.

Would most appreciate any reply, and again thank you.
Lilian

Comments

alex.k’s picture

Assigned: Unassigned » alex.k

You can create a custom block with this PHP code:

global $user;
if (!$user->uid) { return; }
$count = user_relationships_load(array('requestee_id' => $user->uid, 'approved' => 0), array('count' => TRUE));
if ($count) {
 print t('You have !relationships', array('!relationships' => l(format_plural($count, 'one pending relationship', '@count pending relationships'), 'relationships/requests')));
}

When the user has no pending requests, they will not see the block. Hope it works for you.

Liliplanet’s picture

Hi Alex,

That works beautifully! thank you so much .. :)

alex.k’s picture

Status: Active » Fixed

Great :)

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

igorik’s picture

Status: Closed (fixed) » Active

Hi Alex,

Is it possible to get number of only pending requests I got from other users?
Now when I use

<?php
 $count = user_relationships_load(array('requestee_id' => $user->uid, 'approved' => 0), array('count' => TRUE));
?>

I got number of pending requests from other users together with my pending requests what I send to others.
This is not really intuitive. I would like to show only number of requests from others what want to be friends with me and notice user about this (if this number is different from zero).
maybe it could be new value for approved param, e.g. -1: 'approved'->-1

thanks for the help
Igor

birchy82’s picture

Also I made a view to show a small tab that they have a pending relationship request. Works great. So there is 2 options now!

steve.day’s picture

I just had the same requirement as #5 and solved it with the following code:

$pending_relationships = user_relationships_load(array('requestee_id' => $user->uid, 'approved' => 0));
$count = 0;
foreach($pending_relationships as $requester_uid => $relationship) {
  if($user->uid != $requester_uid) {
    $count++;
   }
}

It's a bit more long-winded, but it seems to work.

Anonymous’s picture

Version: 6.x-1.x-dev » 7.x-1.x-dev

I've packaged the solution from #7 into this static cached function for Drupal 7:

function _count_pending_relationships($account = NULL) {
  $counts = &drupal_static(__FUNCTION__, array());
  if (!$account || $account->uid == 0) {
    global $user;
    $account = $user;
  }

  if (!isset($counts[$account->uid])) {
    $count = 0;
    $pending_relationships = user_relationships_load(array('requestee_id' => $account->uid, 'approved' => 0), array('count' => TRUE));
    if (!empty($pending_relationships)) {
      foreach($pending_relationships as $requester_uid => $relationship) {
        if($account->uid != $requester_uid) {
          $count++;
        }
      }
    }
    $counts[$account->uid] = $count;
  }

  return $counts[$account->uid];
}
Liliplanet’s picture

Hi,

I was wondering is it perhaps possible that the menu item or link show 'relationship requests (x)'

Something similar to when you have a private message which shows 'messages (x)'

Looking forward to any reply, and thank you.

mrf’s picture

Component: Documentation » Blocks
Assigned: alex.k » Unassigned
Maks’s picture

Hi,
I totally agree with the Liliplanet (#9)! That would be great!

tanmayk’s picture

@Liliplanet @Maks

You can create your custom menu for that purpose, same like in privatemsg module.
In your hook_menu, you can specify 'title callback'.

Hope that helps.

Regards,
Tanmay

warmth’s picture

@morning_time I'm getting:
Warning: Invalid argument supplied for foreach() in MYMOD_count_pending_relationships()

@Liliplanet & @Maks as @tanmayk said:
Following Drupal Commerce: Alter 'Checkout' link in user menu & Display number of pending messages and friend request on the user menu:

YOURCUSTOMMOD.module

<?php
function YOURCUSTOMMOD_unread_messages(){
	global $user;
	$unread_messages = privatemsg_unread_count($user);
	return $unread_messages;
}

function YOURCUSTOMMOD_count_pending_relationships($account = NULL){
    global $user;
    $pending_relationships = user_relationships_load(array('requestee_id' => $user->uid, 'approved' => 0));
    $count = 0;
    if (is_array($pending_relationships) && !empty($pending_relationships)) {
      foreach($pending_relationships as $requester_uid => $relationship) {
        if($user->uid != $requester_uid) {
          $count++;
         }
      }
   }
    return $count;
}
?>

template.php

<?php
function YOURTHEME_menu_link(array $variables) {
	$element = $variables['element'];
	$sub_menu = '';
	$count = '';
	$name_id = strtolower(strip_tags($element['#title']));  
	
	// add counters
	if ($name_id == 'mis mensajes') {
		$count = sineditmod_unread_messages();
		$count = '('.l($count, $element['#href'], $element['#localized_options']).')';
	}
	if ($name_id == 'mis amigos') {
		$count = sineditmod_count_pending_relationships();
		$count = '('.l($count, 'relationships/received', $element['#localized_options']).')';
	}
	$output = l($element['#title'], $element['#href'], $element['#localized_options']);
	return '<li' . drupal_attributes($element['#attributes']) . '>' . $output . $sub_menu . '<span class="count green">' . $count . '</span>' . "</li>\n";
}
?>

Note: Remember to clean the cache!
Update: Edited to add @tanmayk's suggestion. #15

warmth’s picture

tanmayk’s picture

@warmth You should check whether you are getting an array and it is not empty ;)

function YOURCUSTOMMOD_count_pending_relationships($account = NULL){
    global $user;
    $pending_relationships = user_relationships_load(array('requestee_id' => $user->uid, 'approved' => 0));
    $count = 0;
    if (is_array($pending_relationships) && !empty($pending_relationships)) {
      foreach($pending_relationships as $requester_uid => $relationship) {
        if($user->uid != $requester_uid) {
          $count++;
         }
      }
   }
    return $count;
}
warmth’s picture

@tanmayk roger that, thank you!

TazimHossain’s picture

Title: You have x pending relationships block » Not working for me!
Category: feature » support

I follow this instruction, but not working for me in my drupal 7.22.

I am new in creating module. whats my wrong?

I doing that...
1. Create a example.info
2. Create a example.module

in example.module add this code

<?php
function example_unread_messages(){
    global $user;
    $unread_messages = privatemsg_unread_count($user);
    return $unread_messages;
}

function example_count_pending_relationships($account = NULL){
    global $user;
    $pending_relationships = user_relationships_load(array('requestee_id' => $user->uid, 'approved' => 0));
    $count = 0;
    if (is_array($pending_relationships) && !empty($pending_relationships)) {
      foreach($pending_relationships as $requester_uid => $relationship) {
        if($user->uid != $requester_uid) {
          $count++;
         }
      }
   }
    return $count;
}
?>

and in my themes bottom of the template.php file

function MYTHEMENAME_menu_link(array $variables) {
    $element = $variables['element'];
    $sub_menu = '';
    $count = '';
    $name_id = strtolower(strip_tags($element['#title']));  
    
    // add counters
    if ($name_id == 'mis mensajes') {
        $count = sineditmod_unread_messages();
        $count = '('.l($count, $element['#href'], $element['#localized_options']).')';
    }
    if ($name_id == 'mis amigos') {
        $count = sineditmod_count_pending_relationships();
        $count = '('.l($count, 'relationships/received', $element['#localized_options']).')';
    }
    $output = l($element['#title'], $element['#href'], $element['#localized_options']);
    return '<li' . drupal_attributes($element['#attributes']) . '>' . $output . $sub_menu . '<span class="count green">' . $count . '</span>' . "</li>\n";
}

but nothing change in my user menus "My Relationships" menu.

when i check "Messages" and "My Relationships" menu with firebug, i see "<span class="count green"></span>" this, in bottom of the menu links.

then enable the module and clean all cache.

what i'll do now? please help... waiting for response...

tanmayk’s picture

Title: Not working for me! » You have x pending relationships block

@TazimHossain,

If you are seeing "<span class="count green"></span>" and not the counter ($count) in there, it seems to me that there is some problem with your if condition. Please check once again whether conditions if ($name_id == 'mis mensajes') and if ($name_id == 'mis amigos') are true.

Also no need to change title as 'Not working for me!' ;)

paul_constantine’s picture

Cool block.

But you need to change the link from 'relationships/requests' to 'relationships/received' in order for it to work.

Thanks :-)

<?php
global $user;
if (!$user->uid) { return; }
$count = user_relationships_load(array('requestee_id' => $user->uid, 'approved' => 0), array('count' => TRUE));
if ($count) {
print t('You have !relationships', array('!relationships' => l(format_plural($count, 'one pending relationship', '@count pending relationships'), 'relationships/received')));
}
?>