Purchase our premium subscription and get access to HD videos and our support forum.

Log Site Activity with Message and Rules

The Message module is a general logging utility tool that site builders can use to create user friendly logs. Let’s say for example, you want to create a log for all new “Blog” posts and then display a list of logs on a website via a block within a sidebar column. Message module makes it extremely easy to do all of this without writing a single line of code.

In this article I’ll show you how to setup a message type, create the message logs using Rules and then we’ll display a list of the logs using the Views module.

If you want to see this functionality in action, download and install the example module from GitHub.

Getting started

Before we start, we’ll have to download and enable a few modules. Go ahead and download the following modules:

If you use Drush, run the following command:

$ drush dl message views ctools entity token entityreference rules features strongarm

  • Message
  • Token
  • Entity API
  • Entity tokens
  • Entity reference
  • Chaos tools
  • Views
  • Views UI
  • Rules
  • Rules UI

This may seem like a lot of modules, however most sites that I work on these days the above modules are already installed.

Message types

The first piece of work we need to do is configure the message type and attach a field to this new message type. Message types are entities, so you can attach any type of field to an entity the same way as with content types.

Let’s go ahead and create the message type. Go to Structure -> “Message types” (admin/structure/messages).

Click on the “Add message type” link and enter in “Log new content” as the Description and enter in some placeholder text into the message text field. Scroll to the bottom and click on “Save message type”.

Now we’ll need to add an entity reference field on the message type. We’ll add an entity reference field so that we can reference the created content for later use. 

From the “Message types” page (admin/structure/messages), click on” manage fields”.

 

Create a new field and select “Entity reference” from the field drop-down.

 

Go back and edit the message type that you created earlier. Within the “Token” table, click on “Message” and you should see [message:field-message-content]. We’ll use the title of the created content within our message. 

Change the message text to the following message:

[message:field-message-content:title] has been added by [message:user:name].

When a message log is created, the Message module (thanks to Token) will convert [message:field-message-content:title] to the content title and [message:user:name] to the user who is creating the content. This will all make sense soon, I promise.

Message rule

In the section above we worked on configuring a message type, now we’ll use the Rules module to create the actual message when a new blog post is created. Rules is a module which allows site builders to define specific actions based on an event. For example, you can use Rules to send an email when a user comments on a piece of content and this can all be achieved without writing a single line of code. Rules is a powerful module and if you have never used it before, I recommend you spend a little bit of time learning how to use the module. You’ll be amazed by what you can do with Rules.

Check out the Learn the Rules framework series of screencasts on using Rules from NodeOne if you want to learn more.

To create a rule, go to Configuration -> Rules and click on the “Add new rule” link.

Enter in "Log new content into" into the Name field and select "After saving new content" from the “React on event” drop-down field.

At this point we have simply created a rule for a specific event, this rule will be executed after a new piece of content has been saved. Let's go ahead and add a condition so that the rule only runs if the saved content is a blog post.

Click on the "Add condition" link within the Conditions area.

Select "Content is of type" from the conditions drop-down, at this point the page should reload, and then select "Blog" from the Content types field set and click on “Save”.

We have the event and condition, however, now we have to tell rules what the action should be for the rule. 

Click on the "Add action" link within the Actions, then select "Create a new entity" from the action drop-down. The page should reload and display a drop-down list of entities. Select "Message" and click on “Continue”.

Select "log_new_content" from the Message type drop-down and add “node:author” as the data selector and then click on “Save”.

When we created a message type, we also attached an entity reference field on the message type. Let's go ahead and populate the entity reference field when the message is created.

Click on the “Add action” link and select “Set a data value” from the actions drop-down. On the first data selector we must select the entity reference field in the message log. At this point we are specifying which field we need to modify. Select or enter “entity-created:field-message-content” in the Data selector field and click on “Continue”.

Finally, we must specify which entity or content we want to reference. In our case it would simply be “node”. Enter in “node” in the Data selector field and click on “Save”.

Go ahead and test out the rule. You will see that when you create a blog post, a message log should be created. You can view a list of messages by going to Content -> Messages (admin/content/message).

Display message logs with Views

In the previous section we configured a rule which’ll create a message log when a Blog post is created by a user. As stated before a message log is an entity, this gives us the ability to create a view using Views to display the message logs. In this section we’ll create a simple block view that displays the top five message logs in a sidebar column.

If you have used Views in the past, than creating a block view of message logs will be fairly straight forward. However, let’s quickly go through the steps necessary in creating a block view.

Go to Structure -> Views and click on the “Add new view” (admin/structure/views/add) link.

Enter in a name into the View name field and select “Message” from the Show drop-down and select “log_new_content” from the Type drop-down. Also tick the “Create a block” checkbox and then click on “Continue & edit”.

We’ll keep this view simple and just add two fields. One for the message and another for the “View more” link.

Click on the “add” button next to fields and select the “Message: Render message (Get text)”. By adding this field you should see the render message.

The last field we need to add is the “Read more” link. To add the link we need to attach the Blog entity to this view. 

Still within the views edit page, click on the “Advanced” field-set and then click on “add” next to relationships.

Select the “Entity Reference: Referenced Entity” and click on “Apply”.

On the Configure Relationship page, tick the “Require this relationship” and click on “Apply” again.

Click on “add” next to Fields and search for “link”. Select the “Node: Link” and click on “Apply”.

Deselect the “Create a label” check-box and enter “Read more” into the “Text to display” text field.

At this point the preview should look something like this.

Finally make sure you save the view by clicking “Save”.

Let’s test out the new view block that we just created. Go to Structure -> Blocks (admin/structure/block) and add the block to a specific region.

Just style it with some CSS and you are done. If you have any questions, please leave a comment.

Ivan is the founder of Web Wash and spends most of his time consulting and writing about Drupal. He's been working with Drupal for 6 years and has successfully completed several large Drupal projects in Australia.

Connect: Twitter drupal.org LinkedIn App.net

Comments

Submitted by milesw on

Very, very nicely put together. This got me up to speed in no time. The Message module is a pleasure to work with already. Thanks!

Submitted by Murray on

Thanks for introducing me to this. I have a need for it. Originally I was appending lines to a textarea! The next step for me was going to be to create a logging schema table with some views data magic to handle it. Looks like this module is now a contender. It certainly would be easier to set up. I don't know though - I feel dirty using an entity for something like logging where it will be nice to see it all in one table on the backend. A schema table feels like a better fit. That's a "save it for the pub" conversation though :)

ivan's picture

Submitted by ivan on

I do agree that entities can sometimes be a bit overkill. However, with Rules integration, you don't have to write any custom code.

Submitted by Mark on

Thanks a lot for this great and easy to follow tutorial. Just the explaination I needed!

Submitted by Ramya on

Thank you so much for posting this! This is exactly what I was looking for! But, I have a few questions. I downloaded all the modules, and I set up the message types, but when I try to open "manage fields", it says "You are not authorized to access this page". I'm not sure why I'm getting this message since I am the admin for the site. I looked at the screenshots you had more closely and also realized that the status for the 'log new content' shows as overridden, whereas mine shows up as custom. I am also not using drush. Am I missing some dependency or a step in the process that would allow me to manage fields in the log content

ivan's picture

Submitted by ivan on

I have never seen an "Access Denied" on the fields page before. Have you tried to clear the site cache?

Submitted by Ramya on

I downloaded the dev version of the Message module. I am an admin with full permissions on the site. My uid is 3, not 1. When I try to open manage fields or manage display of a message type, I get access denied. I have the same issue in 2 different websites. Any suggestions as to how I could fix this issue?

Submitted by Bogdan on

Hello,

I was wondering how can I delete this messages. For example I have a feed just like Facebook and I want users to have the possibility to delete each message individually. Can this be done?

Many thnaks

ivan's picture

Submitted by ivan on

Good question.

I have not had a need to delete messages. I don't think Message module gives you a UI to delete message out of the box. You'll have to write some custom code.

Submitted by Melissa on

Does the message module offer similar features as heartbeat module in that users are able to like, comment on activity? Does message module integrate nicely with User Relationships module? Can the activity created with message module work like a heartbeat stream in that the stream can encompass all message types created by all users a member has a relationship with? For example, if i would like to create message types for user creates new content, updates existing comments, becomes follower of user, updates profile, updates account, comments on comment. If I create all message types, can I place them all into 1 stream or view and have that view show all message activity for all users a member has a current relationship with?

Additionally, what other modules social networking modules work well with message module? Any integration with flags, statuses etc.

Sorry for all the questions. Any help is greatly appreciated.

Thanks

ivan's picture

Submitted by ivan on

Message is a lot more generic than Heartbeat. Heartbeat does give you more social functionality. If you want to integrate some social functionality, you'll have to do some custom work.

If Heartbeat does the job than use Heartbeat.

Hope this helps.

Submitted by in a nutshell on

Doesn't it hurt performance?
i mean each activity is a new created node of type message
if i have 10 000 users and i want to log their activity of flag_friends
every user adds for example 10 users a month
then i have new 100 000 nodes a months - just for logging this activity
is it logical? i see it costful

ivan's picture

Submitted by ivan on

You won't really have performance issues unless you have written some massive SQL query or you have a lot of traffic performing INSERT queries. If the site traffic is growing make sure you are upgrading the infrastructure at the same time.

Regarding the flag_friends question. The short answer is yes; the table will grow. Just make sure you have upgraded your infrastructure to handle it.

Submitted by Sivakumar on

I am also evaluating Message module for logging site activity and this is an important concern. But looking into the Message implementation, I see that message is implemented as an Entity rather than node. The overhead in creating entity is not the same as nodes.

ivan's picture

Submitted by ivan on

Nodes in Drupal 7 are entities as well they are just an entity type. Message module simply implements its own entity type so that site administrators can attach fields to the entity. Performance really comes down to how your hosting is setup.

Submitted by Audrius on

Great tutorial, but... How in Message Example module it is made that all of the message creation is dome WITHOUT Rules module?
I am trying to understand this for entire day without luck.

Submitted by Audrius on

thank you! I wasted all day to understand, how it is possible without Rules - didn't knew it is hardcoded.

Submitted by Audrius on

Trying to make everything the same with comments - got it working except that in the part
"Finally, we must specify which entity or content we want to reference. In our case it would simply be “node” I cannot simply select "comment". I think because of this when I delete a comment the message is not deleted, right?

ivan's picture

Submitted by ivan on

First, if you create a rule on the "After saving new content" event, you won't get direct access to the comment entity. The "After saving new content" action is fired when a node is created. Look at using other actions.

If you want messages to be deleted when a comment is deleted, then you'll have to create a rule that'll delete the messages.

Submitted by Audrius on

Hi, I am with the same task again - what you mentioned is understandable, using action "After saving new comment" but then little luck in creation the same (similar) message on comment creation. Could you please recreate this tutorial with the rule of new comment? I am trying for a few days but cannot pass values of comment into, what I do:

Event: After new comment creation
Conditions: none
Action: Create new message entity "After new comment"
Value: comment

Now my message has Entity reference field "field_myfieldname" with Target type - comments, everything basically the same

When looking at the messages list I see only "[message:field-myfieldname:title]" the tokens are empty.

ivan's picture

Submitted by ivan on

If you only get "[message:field-myfieldname:title]" in the message, then that means the referenced comment is not getting attached during the rule execution.

Check and see if the comment is actually attached to the message entity. Also, use the debug action and see if anything is returned if you debug the "message:field-myfieldname:title" data selector.

Go to the link below for a video on how to debug rules:

http://nodeone.se/en/debugging-rules-configuration

Submitted by Audrius on

Thank you, will look at thaht, but I am 90% sure there is some kind of a problem. Have you done the same as in this tutorial with comments? I have a tiny bit of feeling - that there may be some sort of strange limitation as even Commons installation is not offering this.

Submitted by Audrius on

Ivan, I have 10x-double-tripple checked everything, and yes, looked at Rules debugging screencast - it is great, never used this feature before!
Now, debugging info shows that entity is created, action is evaluated, event is fired - no errors or warnings. But the problem is absolutely the same - comment is not passed to message :( I compared to the event when node is created - it looks identical! User created/logged-in/out etc - everything works, everything is just cool, but I cannot do anything with the comment.

It seems you are the only one capable or willing to share your Message knowledge. Can you somehow... save me from my misery and update your article with event on comment creation (and passing its values to message)? Please.

Right now I will try to repeat the same on the clean site - it may be some module causing this

ivan's picture

Submitted by ivan on

I'll look at writing a tutorial about using Message and comments together.

Test what you're trying to do on a fresh Drupal 7 site, very good idea.

Submitted by Audrius on

Man, I've spent 20+ hours just to find, that problem is my messed up comment... fields. Everything works IF I try to pass values like comment date etc - not any of the fields.
Everything just started to work if I repeated everything on fresh clean minimal D7 install. Your tutorial is amazing and your help is invaluable!

Thank you very much. Caseis

Submitted by Audrius on

And... everything just does NOT work again on dev site. It just works on clean install but not on fully stacked site. There seems some weird bug.

Submitted by Rich on

Hi - thanks for the excellent tutorial, i am hoping to use this approach for a site i am currently building. However, i'm finding integration with OG tricky, ie logging messages about content that belongs to a group (and making this group information available from within the message).

Is it possible with Rules, or does it need to be handled in code? I've looked at the message_og_example message which seems to vaguely make sense, however it throws errors when saving a piece of group content...

Cheers

ivan's picture

Submitted by ivan on

Sorry, I don't have any experience with using Message and OG together.

The way I will tackle this problem is with Rules first, if I can't get it working with Rules, then I'll look into a custom code solution.

Submitted by Rich on

FYI i've now managed to implement this - manually in code, loosely based on the message_og_example module.

I initially tried with Rules, but it seems its not able to handle the complex data types (og_membership, og_audience etc)

If you're interested i can come back with more details.
Cheers

Submitted by Amitaibu on

Seems you are very good in explaining stuff ;) -- Would be cool if you could have a post about Message-Subscribe. I have a feeling the Drupal community still doesn't see how this module along with Message-notify can be a powerful and flexible subscriptions and notifications system

-- Amitai(bu)

Submitted by iS on

FYI, since Entityreference prepopulate does not work with OG 7.x-1.x, I just tried Node Reference URL Widget and it worked like a charm. Thanks for the tutorial .. :)

Submitted by PAULAP on

Hi! I have the same problem but don't know what URL parameters I must use to fill the field. Can You give me an example please. Thx :-)

Submitted by Philip Battin on

Thanks for the tutorial. Followed it and still don't see the "Messages" tab under "Content". How come?

The log tells me this:
Rules debug information:
" Reacting on event After saving new content.
0 ms Reacting on event After saving new content.
10.144 ms Evaluating conditions of rule Log new content. [edit]
10.869 ms The condition node_is_of_type evaluated to TRUE [edit]
10.898 ms AND evaluated to TRUE.
" Rule Log new content fires. [edit]
0 ms Rule Log new content fires.
5.95 ms Evaluating the action entity_create. [edit]
7.484 ms Added the provided variable entity_created of type message [edit]
9.01 ms Evaluating the action data_set. [edit]
9.358 ms Rule Log new content has fired.
20.37 ms Saved entity_created of type message.
24.786 ms Finished reacting on event After saving new content.

Which means it should be fine? I can even see the messages have been created in the MySQL. Did i miss something?

ivan's picture

Submitted by ivan on

The log "looks" fine but if you're not seeing any messages then something is not working.

First, make sure the rule is firing off. Add an action called "Show a message on the site". Use this action to see if the rule is working.

Once you know the rule is working, then setup the action to create a message entity.

Submitted by Nigel on

Thanks for the tutorial. Any idea how I owuld exand on this and also display which group the node was posted in?

ivan's picture

Submitted by ivan on

What do you mean by group? Content type?

If so, then just display the content type in the view.

Submitted by Nigel on

Yes. Essentially I want to extend your example to say

[user:name] added [node:title in [group:name]

I just don't know how to get the group name based on your example.

Any help would greatly be appreciated.

ivan's picture

Submitted by ivan on

You should be able to access the group node by selecting it from the data selector within Rules. But, make sure you have the "og_group_ref" field on the node that you're using to create the messages.

Submitted by Blaze on

The message is not logging the second time the rule fires.
For example I have a content type "Sample".
The goal here is to log a message every time a new content of Sample content type is created.
I have have create a message type for the Log message and created a field entity reference of node type "Sample"
Then created a rule to trigger entity creation of and setting the data of the created entity.

When I created a content of node type Sample for the first time. The message logs and displayed on view.
But when I create a content of node type Sample again. The message wont log anymore.
Please help me with my problem. Thank you.

Submitted by Ben Chesters on

I see how this works now and it's very good for its purpose. But how do I incorporate it dynamically into a view, I need to 'attach' the log time to the node it is associated with; in my case an updated field. I want to have a table with 'name of publisher', 'publisher url' and then 'time and date publisher was paid'. Would I need to add an entity reference field to the actual message type then try to access it through views? Thanks for the info so far, I am getting there!

ivan's picture

Submitted by ivan on

Yes, add an entity reference, publisher and publisher URL to a message type. The same as you would on a content type. Then use Views to create a basic table.

Submitted by benchesters on

I created a message, exactly as you have here and it works fine. I have now tweaked the rule so it fires when (1) the content type updated is 'campaign' [as the publishers are referenced by this when they work on a campaign] and (2) the field of a reference publisher is changed to 'PAID' status. The rule fires perfectly but the message displayed it ignores the tokens I used and just prints the placeholders! Am I supposed to attach more than one field to the message type? At the moment I just have 1 entity reference field which I assumed would reference any other content type.

ivan's picture

Submitted by ivan on

I don't know why it's not working. If you're trying to process a token value from a content type that is reference via an entity reference field, then you may have to add an "Entity exists by property or Entity has field" condition to your rule.

Pages

Add new comment