Usually, you can have mentions with e-mail notifications using the Drupal mentions module, or mentions with auto-suggest using the Drupal ckeditor_mentions module, but not both. But it turns out you can, indeed, by combining both modules (see also). Here are detailed instructions how to set thus up.
Recommended: Using Message Notify for message sending
- Install and enable required modules: rules, mentions, ckeditor_mentions, message_notify.
- Configure the mentions module (in /admin/config/content/mentions) to use "Input: Prefix: @" and "Input: Suffix: " (nothing). This corresponds to how usernames are displayed after letting ckeditor_mentions insert a suggestion.
- Make sure your usernames are set up to not contain spaces, because the @username format does not allow usernames with spaces to be recognized.
- Enable permission "Bypass Rules access control" for your user's user group, or you will get a "Access violation! You have insufficient access permissions to edit this configuration." error when trying to create the "mention:entity of type …" Rule condition below.
- In the admin menu, go to "Structure -> Message types" (/admin/structure/messages) and create a new message type. It is simplest to clone it from one that is meant for use in e-mails already, such as any Commons Notify message type (if you are a Drupal Commons user).
- Edit your new message type to your needs. You can use the following replacement tokens in both subject and body fields:
- [message:user:name] Username of the user to whom the message is sent.
- [mention:author:name] Username of the user who created the mention.
- [mention:entity:…]
- Create a new Rule (say, named message_on_mention_in_node) like this:
- Specify the rule's trigger to be: "React on event: After a new mention is created".
- Add a condition specifying "mention:entity is of type: Node".
- Add an action "Create a new entity". [TODO]
- Add an action "Provide a data value". [TODO]
- Add an action "Save entity". [TODO]
- Add an action "Send message with Message Notify" [TODO]
- The four actions in your rule should now look similar to this screenshot.
- Create a new Rule messgae_on_mention_in_comment, in analogy to the above but with the condition using entity type "comment".
Alternative: Using PHP code for message sending
It is also possible to create a rule that will send the required e-mail using custom PHP code. However, since this requires enabling the "PHP Filter (php)" module, it is not considered good practice security-wise. However, if you want to use this route, you can do so as follows:
- Follow steps 1-3 from the above list.
- Create a Rule email_on_mention_with_php.
- Specify the rule's trigger to be: "React on event: After a new mention is created".
- Add an action "Send mail", set its "To:" field to "mention:user:mail" and the Subject field to something similar to the following text:
Hello [mention:user:field-name-first] [mention:user:field-name-last],
you got mentioned by [mention:author:field-name-first] [mention:author:field-name-last] here:
<?php
if ($mention->entity_type == 'comment') {
echo '"' . $mention->entity->subject . '"' . "\n";
echo $mention->entity->comment_body[‘und’][0][‘safe_value’];
}
elseif ($mention->entity_type == 'node') {
echo '"' . $mention->entity->title . '"' . "\n";
echo $mention->entity->body[‘und’][0][‘safe_value’];
}
else {
echo '<no preview available>';
}
?>
You can view the mention and comment on it here:
<?php
if ($mention->entity_type == 'comment') {
$cid = $mention->entity->cid;
echo("http://edgeryders.eu/comment/$cid#comment-$cid");
}
elseif ($mention->entity_type == 'node') {
$nid = $mention->entity->nid;
echo("http://edgeryders.eu/node/$nid");
}
else {
echo '<no URL available>';
}
?>
best regards,
xxx
<?php
// echo("——————-\n" . 'Debugging information: $mention = ');
// print_r($mention);
?>
Leave a Reply