I need a specialized version of "Send to a friend" for my site, and I'm planning on making myself a custom module with mymodule_form_privatemsg_new_alter (as suggested in #382756: Allow to set subject and body with query arguments).

Is there any way I can set a tag on a message when it is created? My use case is:
Bob goes through the site every day and "send to a friend"s a bunch of nodes to a bunch of users. At the end of each month, Bob wants to be able to run a simple filter search and see a list/report of the various nodes that he has recommended to other people in the last month. (I was planning on using the same static tag "referral" for all the messages.)

It seems like tags are a perfect way handle the filtering (yeah!) but I don't want poor theoretical-Bob to have to go through and manually tag these messages.

Thanks!

Comments

litwol’s picture

Interesting concept, please expand on it further. In short however, this feature does not exist yet but the way module is engineered it would be rather easy to extend it to this functionality.

Please continue to elaborate the exact user experience of this feature. Where would the link to recomend appear? when clicked what happens? at some point user arrives to 'write new message' window, assuming there was a tag field what would this field be prepopulated to? will it be prepopulated? etc etc. please try to describe the complete user experience with this feature.

leenwebb’s picture

Well, this is only MY use case, but here's what I'm aiming for:

The "Recommend" link would show up when a user looked at a node (or node teaser). I guess by default it would be in $links? In my case I am creating a custom .tpl so it will display elsewhere, but it's meant to take the place of some generic "send to a friend" link.

When the link is clicked, it would go to /messages/new/?destination=/node/123&tag_id=1. (One could definitely use the snazzy /messages/new/%/custom-subject if that met one's needs. In my case I need to pre-populate the body field and there's no pre-set recipient, so I'll form_alter the whole message myself.)

When the user is at the "Write new message" screen the "Tags" section is there! Since &_REQUEST['tag_id'] = 1, the tag with ID 1 is already checked. The user can add more tags or uncheck or whatever, but usually they'll just leave it alone.

They send the message, and it is already tagged! Then later on they can filter/etc, just as if they had gone into Sent Messages and tagged it after the fact.

--

So I guess that this all comes down to including the Tags section in the "Write New Message" form, and also exposing that to the form_alter so that tags can be pre-checked if desired.
(I am guessing that the trick comes in tagging a message that hasn't been created yet.)

berdir’s picture

I like that idea. It would make sense to add that Tag field in a hook_form_alter in privatemg_filter, because that's where the tagging stuff lives. So you would have to give your custom module a higher weight, but that's easy to do.

Regarding the handling of that information, we do have several hooks, however, the tricky part is to get that custom information into the $message array, so that you can access it in hook_privatemsg_message_insert(). Probably a custom validate function (that's needed anyway, to validate those tags), and that could add it to $form_state['validate_built_message']. But that requires changing the weight of the filter module and is not as generic as I'd like to have it. It should be as easy as adding a field to the form and then access it in those hooks, validate, submit and so on. As it works for node.module.. I have to think about that ;)

If you want to help, you could start with a patch that adds such a field with privatemsg_filter_form_alter. I will probably do it myself, but I'm in the middle of my exams and don't have much time at the moment.

It seems like tags are a perfect way handle the filtering (yeah!) but I don't want poor theoretical-Bob to have to go through and manually tag these messages.

Finally, someone likes tags! :)

leenwebb’s picture

An update --

I ran out of time and I fear I am not equipped with the knowledge as to how to do this stuff correctly. So I did it a hacky way, which is totally functional for my use case but not necessarily translatable to others' needs.

I did this:

  1. Created a mymodule_form_privatemsg_new_alter, which looks for $_REQUEST['nid'] and creates a subject and body based on the node info.
  2. I also copied into that module almost the entirety of privatemsg_filter_form (I removed the thread_id, user_id, and submit buttons). I changed the form -- instead of $form['tags'] it's $form['privatemsg']['tags'] so that the fieldset sits right underneath the message body and is submitted with that form.
  3. The tag I care about has a tag_id of 1, so I set that to "checked" if nid is present. This is obviously not useful for any use case but mine. This is a case of me just wanting this done, instead of figuring out the proper way to do it so that anyone could use it.
  4. I put the above filter_form stuff inside an if (!isset($form['privatemsg']['thread_id']['#value'])) so my fakey-tag section only shows up on a brand new message.
  5. Inside _privatemsg_send, I added a little section that loops through the possible tags and if it finds $_REQUEST['tag_1'] or ['tag_2'] and so on, it inserts the DB entry so that thread is tagged. Oy, that is hacky.

So! If someone else needs to figure out a way to tag messages as they are sent, this is one way you could do it! It is not the prettiest and is most certainly not upgrade-safe, but it works!

berdir’s picture

Status: Active » Closed (duplicate)

Rules module does now support adding tags to a thread. See #327938: Rules Integration. It might not yet support that specific use case, though but I'm setting this to a duplicate anyway.

Feel free to re-open if you think that you need another solution.