0

I know that this question had been asked several times, but it seems like all existing answers only apply to ealier versions of Privatemsg module. I am running 7.x-2.x-dev and I would like to programatically render the "Send this user a private message" link. The PHP code is supposed to be the $block['content'] of a programatically created block that will be displayed on user pages.

Somebody suggested to use

if ($account = menu_get_object('user') ) {
      if ($url = privatemsg_get_link(array($account))) {
        $block['content'] = l(t('Send this user a private message'), $url, array('query' => drupal_get_destination()));
      }
}
but this is doing absolutely nothing.

I also want that the link is only displayed if the viewed user has enabled the "Enable private messages" option in his/her account settings and if the viewing user has permission to send messages. Any ideas how to achieve this?

  • If "somebody suggested" something, you should link to the source or make it clear why you cannot (read in IRC, etc.) – Patrick Kenny Aug 1 '13 at 1:23
1

For an example of how to do this, you can see the integration with the Author Pane module in privatemsg.author-pane.inc:

  global $user;
  $account = $variables['account'];

  if ($account->uid != $user->uid) {
    $url = privatemsg_get_link($account);
    if (!empty($url)) {
      $variables['privatemsg'] = l('<span>' . t('Send PM') . '</span>', $url, array('attributes' => array('class' => array('author-pane-link'), 'title' => t('Send @name a private message', array('@name' => privatemsg_recipient_format($account, array('plain' => TRUE))))), 'html' => TRUE));
    }
  }

This isn't a cut-and-paste solution, but since Author Pane provides a block that allows you to PM given users, and this respects the Privatemsg permissions (the link should not be rendered if the recipient does not have appropriate permissions to receive or the sender does not have permission to send), this should be a useful reference.

  • Thank you, I managed to modify this code to get it to work in my case. – deinqwertz Aug 6 '13 at 9:18
2

Hope this will help you,

In Drupal 7, you can use privatemsg_get_link.

  • privatemsg_get_link - Returns a link to send message form for a specific users.
      if ($enabled_private_messages) {//check user private message settings
        if ($account = menu_get_object('user')) {// Get user object. You can also use user_load to get user object.
          if (is_object($account) && privatemsg_user_access('write privatemsg')) {
            if ($url = privatemsg_get_link(array($account))) {
              return l(t('contact user'), $url, array('query' => drupal_get_destination()));
            }
          }
        }
      } 
    
  • This answer is obviously also correct, thank you very much! However, I decided to accept the other one, since it provided me some more information how to implement the function. – deinqwertz Aug 6 '13 at 9:19
  • Its ok.. You got the answer right, that's enough. – inizio Aug 6 '13 at 11:03
  • This can be shortened by removing all the conditionals before if ($url = privatemsg_get_link($account)) …. The checks are done by the privatemsg_get_link function. – thirdender Jun 13 '14 at 1:12

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.