1

I'm using Privatemsg & User Relationships and the built-in user menu as a sidebar block. What I want is to display the unread messages count and the pending friends requests beside the links "My messages" & "My friends".

Any ideas of how can I do this? Thanks in advance.

Mock-up:

User Menu Mock-up

2
  • ate you using drupal 7 ? Nov 30, 2012 at 4:27
  • Yes, I am on Drupal 7.17!
    – warmth
    Dec 1, 2012 at 2:53

2 Answers 2

2

This will return the unread private messages in Drupal 7.

<?php
global $user; privatemsg_unread_count($user);
?>

Refer this for more discussion

This will return pending relationship request for the loggedin user.

<?php
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];
}
?>

Refer this for detail discussion.

You can create a block and create respective links with the functions shown above.

2
  • Do you think I can hook the existent user menu block to use those function you provided? What should I modify? I'm a beginner about modifying Drupals Modules behaviors by hooking or preprocessing. Thank you for all your help!
    – warmth
    Dec 1, 2012 at 2:51
  • Just do one thing, create new simple block module and display all the data. Dec 1, 2012 at 6:34
0

At the end @mohit_rocks answer was useful but didn't work at all. Here's my workaround (based in what I learned from Drupal Commerce: Alter 'Checkout' link in user menu & You have x pending relationships block - note that I posted this answer on the second too):

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!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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