2

I'd like to use Privatemsg module to send messages to group of users. For this purpose I plan to use a view with Views Bulk Operations (VBO) and plenty of exposed filters.

I was able to create a custom VBO-action that puts the selected users into the recipient field of the message form:

function mymodule_my_custom_action(&$user, $context=array()) {

module_load_include('pages.inc','privatemsg');

$recipients = array();
foreach ($user as $recipient) {
$recipients[] = user_load($recipient->uid);
}

$subject = 'Some subject'

$message_form = drupal_get_form('privatemsg_new', $recipients, $subject);
$message_html = drupal_render($message_form);
}

I'm not very experienced in development so I don't know how to display this form together with the view that helps choosing the recipients. A simple print $message_html; won't be enough of course. For example would it be possible to use a panel page containing two panes: one for the view of users with VBO and one for the privatemsg form?

Any help would be greatly appreciated!

1 Answer 1

2

As per the "I'm not very experienced in development", you may want to use an alternative approach which only requires site building skills, as further explained below ...

Use the integration of the Privatemsg module with Rules instead (make sure to checkout this community docu, and its child pages ...), together with the Views Bulk Operations module.

For more details, refer to the video about Using Rules components with VBO. Possibly you also want to look at the 2 prior videos and/or the 2 subsequent videos (all about Views Bulk Operations), i.e.:

In this specific case;

  1. use Views bulk operations to create the list of your users to be targeted (so this is just a minor variation of the VBO based view you seem to already have in place).
  2. create a Rules Component which uses Rules action "Send a message" (provided by the Privatemsg module). If you'd add 2 "variables" to this Rules Component, named something like "MsgSubject" and "MsgBody", you might want to use those variables as "tokens" for constructing the actual Subject and Body of your message.
  3. Within your VBO settings, just execute this Rules Component. If you then use this VBO-view, you can specify the desired values for the variables used to create your Rules Component (like "MsgSubject" and "MsgBody"). So you avoid hardcoding the "Subject" and "Body" for the "Send a message" action (without those variables it would only allow you to send predefined messages). And this simply by using these variables of your Rules Component (to implement that "Subject" and "Body" of the message). That way, each time you use your VBO to send such messages, you can enter the desired "Subject" and "Body" for each of those messages.

Done, no (custom coding) involved ...

For some other illustrations about using VBO in combination with Rules Components, refer to the answers to these questions:

7
  • Thanks for the detailed answer. But - unless I'm mistaken - the "Send a message" action needs to specify subject and body, so I can send predefined messages only. Or is it possible to use this method in sending any messages in privatemsg message form?
    – StG
    May 6, 2016 at 15:04
  • @StG: Good (extra) question! Refer to my Bonus tip I just added to also address that (and some related questions to get a better idea of how it is like to use Rules with VBO). The possibilities you get by the combination of both amazing modules is only limited to your own imagination ... May 6, 2016 at 15:18
  • That seems to be a good solution but I don't really know how to implement it. To have subject and body values as variables, users needs fields to fill. How can I add these two extra fields to the VBO-page and hhow can I submit them?
    – StG
    May 13, 2016 at 8:20
  • @StG : refer to my updated answer (the numbered list items 2. and 3.) And make sure to watch the videos (especially the one about Rules components with more than one parm). Let me know (via extra comment) if you can get it to work, ok? May 13, 2016 at 8:43
  • Ah okay, I see now. Thanks a lot, I 've created the rule component with 3 parameters (list of users, text for subject and formated text for body) and it's working! Thanks again! However, I still have a minor issue with it: Since I send the messages looping through the list of users, privatemsg module sends individual messages and doesn't create a thread. Is it possible to use the whole array of users in a single Send message action?
    – StG
    May 14, 2016 at 7:18

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.