I have a flag on my Drupal 7 site for alerting moderators regarding potential abuse.

I want a link to this flag to be displayed next to the Delete and Block links on the view private message screen (messages/view) provided by the Private Message module.

By inserting the following code below the code for the delete link (line 769) in the latest version of the module, I can render the link correctly.

$author = $vars['message']->author;
$vars['message_actions'][] = array('title' => t('Report abuse'), 'href' => 'flag/confirm/flag/abuse/flag/block/' . $author->uid .  '?destination=user/' . $author->uid );

However, I don't want to hack the module; I want to add this code to a custom module. hook_privatemsg_message_view_alter() appears to be the function for this, but I don't understand how to use it. Simply inserting the above code and calling the function from within my custom module does not result in any errors but the link is not displayed. What do I need to show the link?

EDIT: The solution need not use hook_privatemsg_message_view_alter(); if there is a better way that doesn't involve hacking privatemsg, I'd be happy to hear of it.

  • Does the private message admin interface (messages/view) have an editable views?
    – WestieUK
    Commented May 15, 2012 at 7:42
  • 1
    No, Privatemsg does not integrate with Views/Panels. If it did, I would be able to do this quickly myself. Commented May 15, 2012 at 13:00

2 Answers 2


I really don't know why you don't like the answer by Rene.

Let me show a more detailed example. You have to replace YOURMODULE with name of your module or theme.

 * Implements hook_privatemsg_message_view_alter().
 * The & for param $vars is important: we pass it by reference.
function YOURMODULE_privatemsg_message_view_alter(&$vars) {
  // Message author
  $uid = $vars['message']->author->uid;
  // Get flag for abusing user
  $flag = flag_get_flag('abuse_user');
  // Make flag module to check permissions
  if ($flag->access($uid)) {
    // Use flag API to show a flag according to flag preferences
    $vars['message_actions']['flag-'. $flag->name] = array(
      'title' => $flag->theme($flag->is_flagged($uid) ? 'unflag' : 'flag', $uid),
      'html' => TRUE,

Here I'm using a flag with name 'abuse_user':
enter image description here
You can edit Flag labels, link type and permissions here.
The result:
enter image description here

Please, feel free to ask questions.

  • Thanks, this more detailed explanation is what I needed. I've got it working now with your help. Commented May 19, 2012 at 12:59

Hooks are a way of extending contrib modules without having to hack them. It's saying when this function runs in the the contrib module run all other functions that match the hook signature more or less (there would be a more accurate and concise explenation on drupal.org somwehere).

You need to add the code to you custom .module file and replace the word hook with your custom module name ie..

function my_custom_module_name_privatemsg_message_view_alter(&$vars) {
  // add a link to each message
  $vars['message_links'][] = array(
  'title' => t('My link'),
  'href' => '/path/to/my/action/' . $vars['message']['mid'],

Your Answer

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

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