0

Is it possible to create a link to send private message to CURRENT USER in the node (to the author of the node content to be more precise). I'm using the Privatemsg module. I was trying to do this with the Display suite dynamic fields, but I am not able.

I found that the Privatemsg module comes with an option to "Display link on the selected content types. So, I have the "contact current member link" right in the content type, but it shows only for logged in users.

Is there any configuration or hook that I can implement to show the same link to anonymous user?

This is the code that shows the link.

function privatemsg_node_view($node, $view_mode) {
  $types = array_filter(variable_get('privatemsg_link_node_types', array()));
  $url = privatemsg_get_link(user_load($node->uid));
  if (in_array($node->type, $types) && !empty($url) && ($view_mode == 'full' || (variable_get('privatemsg_display_on_teaser', 1) && $view_mode == 'teaser'))) {
    $node->content['links']['#links']['privatemsg_link'] = array(
      'title' => t('Contact member'),
      'href' => $url . '/' . t('Message regarding @node', array('@node' => $node->title)),
      'query' => drupal_get_destination(),
      'attributes' => array('class' => 'button radius privatemsg-send-link privatemsg-send-link-node'),
    );
  }
} 

To make it clearer, I don't want anonymous user to send messages. What I want is just a link to log in for anonymous users. Expressing it with pseudo-code...

global $user;

if (isset($user->roles[1])) {
  // Do not show button to Anonymous user and show something else or a button to log in (THE CODE FOR LOG IN)
  echo "something else";
}
else {
  // Show button to logged in user (the code of "Send author a message" button)
}
13
  • look at this and this post...you can find the answer, or at least can be helpful...
    – mixerowsky
    Commented Jul 22, 2013 at 23:38
  • mixerowsky, thank you alot for your answer. The links you have shared is very helpful, anyway, i found that privatemsg module comes with an option to "Display link on the selected content types", and with that, you have a link "Send author a message" right in the content type or node that you want. This link can be customized with css and the text can be altered with stringoverrides‎ module. Thank you bro ;)
    – ottosatto
    Commented Jul 23, 2013 at 0:22
  • Hello again mixerowsky. Do you know how to show that link to anonymous user also? Thank you
    – ottosatto
    Commented Jul 23, 2013 at 3:25
  • @user1754562 Privatemsg links don't work for anonymous. There is an outstanding feature request for this: drupal.org/node/1988848 Commented Jul 23, 2013 at 5:05
  • yes, it is impossible to send private message to anonymous user, that user is not registered...
    – mixerowsky
    Commented Jul 23, 2013 at 11:00

1 Answer 1

1

I did it with a Display suite custom code field. With it I am able to use some PHP code.

global $user;
// If the user is anonymous (role=1) show this; otherwise show nothing.
if (isset($user->roles[1])) {
  print l(t('Contact member'), 'user/login', array('attributes' => array('class' => array('radius', 'button'))));
}

So, when the user is logged-in, this code above will be replaced with the contact member link (Send author a message).

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.