Assign user to a node - send email notification when saving

sin.star - October 22, 2009 - 10:39

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

Combining different existing modules

sin.star - October 22, 2009 - 14:12

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

Hi, great tutorial, thanks!

Summit - November 12, 2009 - 13:16

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

Greetings,
Martijn
if you appreciate e-development work, please place a link from your site to www.trekking-world.com

Hey Martijn

sin.star - November 12, 2009 - 23:26

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

 
 

Drupal is a registered trademark of Dries Buytaert.