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.
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
- Entity API
- Entity tokens
- Entity reference
- Chaos tools
- Views UI
- 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.
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
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.
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.
Like what you see?
Join our free email list and receive the following:
- Discover our best tutorials in a 6 part series
- Be notified when free content is published
- Receive our monthly newsletter