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

  1. Install and enable required modules: rules, mentions, ckeditor_mentions, message_notify.
  2. 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.
  3. Make sure your usernames are set up to not contain spaces, because the @username format does not allow usernames with spaces to be recognized.
  4. 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.
  5. 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).
  6. Edit your new message type to your needs. You can use the following replacement tokens in both subject and body fields:
    1. [message:user:name] Username of the user to whom the message is sent.
    2. [mention:author:name] Username of the user who created the mention.
    3. [mention:entity:…]
  7. Create a new Rule (say, named message_on_mention_in_node) like this:
    1. Specify the rule's trigger to be: "React on event: After a new mention is created".
    2. Add a condition specifying "mention:entity is of type: Node".
    3. Add an action "Create a new entity". [TODO]
    4. Add an action "Provide a data value". [TODO]
    5. Add an action "Save entity". [TODO]
    6. Add an action "Send message with Message Notify" [TODO]
    7. The four actions in your rule should now look similar to this screenshot.
  8. 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:

  1. Follow steps 1-3 from the above list.
  2. Create a Rule email_on_mention_with_php.
    1. Specify the rule's trigger to be: "React on event: After a new mention is created".
    2. 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);
?>

 

2 thoughts on “How to create @mentions with auto-suggest and notifications in Drupal 7?

  1. Thank you for the post. I have installed mentions module 7.x-2.x dev. I have created a mention successfully. I want to create hashtags functionality. This version allows to create other input filters as well, but I do not know how to use it to create hashtags. I also installed hashtags module, but it is not working on custom forms.

    Any suggestions?

  2. Hmm, but a hashtag feature is just something like a feed aggregating all content that contains the hashtag, no? So basically like search results for #keyword. In that case, @mentions functionality would not help, as it is geared towards making an action happen when somebody puts a certain element in the content. It can be configured to recognize a hashtag, but there is no meaningful action to be done when a hashtag was “created”. Ok, except perhaps creating a Drupal taxonomy term with that hashtag’s name, and tagging the post accordingly. That would give you the benefit of a big list of all available hashtags, to browse through, in case you want that.

    So in case you want to go down that route, we have a better @mentions implementation for Drupal now, explained here.

    Otherwise, a hashtag module is pretty sure to be a better basis …

Leave a reply

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>