Community & Support

Assign user to a node - send email notification when saving

Dear Drupal community,

NOTE: This is not a question for direct help but rather a possible layout for a solution. Just in case reading it would waste your time, so be warned. ;)

I've been working with Drupal for the last six months and developed a few rather small modules for an institution website. One of these modules is defining a content type (with about 30 additional fields) and is featuring the possibility to assign a node of this content type to a specific registered user. When assigning a node to an user and saving the node, an email is send to this user to notify him/her of the assignment (so he/she can do whatever is necessary next). Only the author of the node and this assigned user are allowed to edit the node (besides the super admin, of course).

The institution found this possibility very helpful and wants to set it up for _all_ content types (around 15 different ones are defined). They also not only want a dropdown with all registered users but additionally a text field where a short message can be entered (i.e. a description of the next task) which shall be included in the notification email.

Now I don't want to define all content types by writing separate modules for each of them (since most of them have standard layout [title/body] without any additional fields). So my plan is either to use a combination of existing modules to achieve this or to write a new module that adds the requested features to content types (in general).

By googling and searching on drupal.org I found several suggestions to assign an user to a node with the appropiate permissions.
By using CCK, a user reference field could be added to each content type. The access to nodes could be managed by Node Access User Reference module.
By following this short guide it should be possible to notify the referenced user by mail. By adding an additional field (textarea) to the content types a notification message could be entered by authors/editors and also be used by the mail action trigger combo. Further, the additional fields should of course be hidden for normal display as they are intended for internal workflow. This may be done by using simple CSS.

So, I'll try that first, of course, prior to starting a new module. I'll post my experience here in case others seek similar solutions (i.e. see here, here, here and here).
However, if anybody can think of a different approach which may be easier, faster, more logical or whatever, please point me in the right direction.
Further, if anybody thinks this functionality could be a module of its own (and then maybe adding features such as pre-defined messages/tasks, multiple user assignments and more) please post a comment here and what you would like to see in such a module.

Thanks for reading,
sin

Comments

Combining different existing modules

Going beyond the horizon...

Prerequisites
  • Drupal 6 installed, set up and ready to use
  • Module CCK (including userreference) installed and enabled
  • Module Rules installed and enabled
  • Module Node Access _and_ Node Access User Reference installed and enabled (both modules are required due to NAUR removing default view access rules)


Note: Other modules may be required by those modules.

Step-by-step setup
  • Navigate to 'Manage fields' page of content type in question (i.e. http://yourdomain.com/admin/content/types/page/fields
  • Add two new fields (userreference, text), enter required information at your needs, as required give the referenced user the right to edit the assigned node
  • Go to 'Display fields' and exclude both field from teaser and full node (optional, but I've done because of internal character of information)
  • On permissions page (http://yourdomain.com/admin/user/permissions) enable required roles to edit and view both created fields
  • Create a new triggered rule (/admin/rules/trigger/add), use 'Content is going to be saved' as Event
  • After saving this first part add condition 'Content has type' and choose the content type (i.e. page)
  • Add a second condition 'Field has changed' and check for the userreference field. Indent this condition (icon at end of condition label) and add another condition to group which checks if the message for the assigned user has changed. Any actions of this point is optional.
  • Add an action 'Load a referenced user', the appropiate field should be preselected
  • Add another action 'Send a mail to a user', recipient is the user just identified by loading from userreference
  • Set up the mail options (see mail text example below)

    Hello [referenced_user:user],

    on [node:site-name] website ([node:site-url]), _[user:user]_ has saved the node entitled [node:title] and assigned it to you along with the following message:

    "[node:field_assign_note-formatted]"

    Please visit [node:site-url]/node/[node:nid]/edit to work on the content assigned to you.

    Best regards,
    [node:site-name]'s web administrator

And that's pretty much it. Repeat the steps for all content types you want to have this feature on and you're done. I think this a pretty neat combination of modules.

Regards,
sin

-- formerly known as sin.star

Hi, great tutorial, thanks!

Hi,
great tutorial, thanks! Add on can be to use the cck email field, and it it to the rule.
I do not understand why you need extra access modules though?
greetings, Martijn

Hey Martijn

Hey Martijn,

for the actual goal of sending a note to the assigned user upon saving a node I indeed do not need to use any access control module. But in my special case this actually the origin of the whole idea. I have content types which only users can edit who either created it or have are assigned to it. So without checking for newly assigned content every day the user gets notified by mail just upon the time when a node is assigned to him/her.

Best,
sin

-- formerly known as sin.star

W O W!!!!

It took me hours to find this. Thanks much... Can you do a quick guide to make the user-referenced to automatically subscribe to the node?

Also, where or how do I fix removing the html tags that are showing up in emails?

Thanks in advance for your time...

Local:
Mac OS 10.5.8
Firefox 5.0.1

Server:
Apache 2.0.63
PHP 5.3.6
MySQL 5.0.92-community

Hey zeta, I'm not quite sure

Hey zeta,

I'm not quite sure what you want to achieve. My guide above makes nodes of a specific content assignable to registered users. Users are notified by mail upon assignment. There is no function for subscribing yet (as it wasn't planned for this specific issue).
Regarding the mail I am not using HTML mails. Just switch to plain text editor when defining the mail text.

Best,
Paul

-- formerly known as sin.star

Subscribe

Subscribe

Ideas by Nature
www.ideasbynature.com

Hey Sin, Great post. I got

Hey Sin,

Great post. I got everything set up properly and created a test user to receive a mail, but It's not arriving. I get no errors at all. I checked my recent log entries and the logs say that it has successfully been sent, but the reality is it's not arriving to it's destination

Basically my site has the admin creating lab results for various users/clients. The admin creates a Lab Result which has a user ref, date field, content field, and file field. Admin can see all results, individual user views are limited to their own user referenced records. I followed your documentation to get an email out every time a user gets a new record added and for all appearances in drupal, it looks fine, but the email never is sent out.

Any ideas on what I might be doing wrong? It would be greatly appreciated!

Thanks,
Eric

I think I figured it out. I

I think I figured it out. I just moved my install to a different server and it worked. After doing some digging and testing, I think it's most likely the previous server wasn't configured to use PHP mail().

.

I'm glad you did figure it out.
My solution above is now running for almost a year on a clients site and they are quite satisfied as this fastened up their work flow significantly. Since everything is done by existing modules (so I didn't have to write a single line of code) it is probably in the scope of rules module to check whether PHP mail() function is set up correctly or not.

-- formerly known as sin.star

this guide was excellent,

this guide was excellent, this is the exact functionality my client was looking for.. nice not having to think for myself!

How would you build a

How would you build a condition like that into this rule:

- the node who is created is a og group post. (no condition)
- send the notification mail only, if in the group node a checkbox is checked! (not in the user referenced node!)

I'm not quite sure I

I'm not quite sure I understood that correctly (maybe since I haven't worked with OG,yet): You don't want to choose an user but have a checkbox or shall the checkbox be added and work as a trigger?

-- formerly known as sin.star