Skip to main content

The Cheeky Monkey Media Blog

A few words from the apes, monkeys, and various primates that make up the Cheeky Monkey Super Squad.

Yan Yong

How to Build an Activity Stream by Using Message Module

What does it do: the message module logs system events which can be used to create user activity streams.

Skill Requirement:

  1. Be able to create a list using views module
  2. Be able to use rules to set up triggers
  3. Be able to create new content type and add fields
  4. Understand what view modes are

Modules required:

What we are going to build:

We are going to build a block that lists all recent activities of a given user. It looks like this:

John has created a post “10 things wordpress can’t do for you” - Apr 21st, 2014

John has created a post “Our logo design process - Cheeky monkey media” - Mar 30th, 2014

John has updated a blog post “Why I need to learn message module” - Feb 27th, 2014

General steps:

  1. In order to log system events, we need to setup a message to capture data first.
  2. We need to tell Drupal when and how to log the system event.
  3. Create a block using views to display logged messages to the user.

Detailed instructions:

Assumption: to get a working Drupal installation with a clean url, and the required modules installed and enabled. I also assume you to have the blog post content type configured.

Step 1: Setup Message

As we can see from the above table, in our Message we need to capture the following data:

  • user name
  • blog post URL
  • blog post title
  • timestamp.

We are going to store all the text from the first column of the table into a Message text field, and timestamp in date field. Ok, enough talk, lets configure the blog message.

  1. Go to /admin/structure/messages
  2. Click Add message type, give it a name called {blog message}
  3. In the MESSAGE TEXT section, add the following text:[message:user:name] has created a post "10 things wordpress can’t do for you"
  4. Since we put some static text in the message field, we need to come back later to make it dynamic, but for now we can just carry on.
  5. Click manage fields of the blog message we just created
  6. We are going to attach this blog message to the blog content type, so we can get the blog title and url information. Type Blog reference in the Add new field section, and select Entity reference in the FIELD TYPE selection list. Then finally select Autocompleted in the WIDGET selection list and click Save.
  7. In the next screen, make sure you select Node for the Target type selection list, and check Blog post (your blog post content type) in the Target bundles section. Click Save.You will need to repeat this step in the next page, don’t worry it’s normal.
  8. Now, our Blog message is attached to the Blog post content type, which gives us some data to work with. Click edit link of the Blog Message, and replace the text to the following - [message:user:name] has created a post "[message:field-blog-reference:title]"
  9. Now, we need to store timestamp data in a post date field, although the message entity comes with a timestamp, I find it easier to work with the timestamp store in a separate field. Now let’s add a post date field to the blog message entity. I use Date (ISO format) and WIDGET use Text field.
  10. For the last step, we need to ensure that Message text and Post Date field are visible and the Blog reference is hidden. Click the manage display link of Blog message, then make sure all the field visibilities are set up correctly.

Step 2: Setup rules to log events

In this step, we are going to tell Drupal to create a new blog message instance (when a blog post was created or updated). We also need to send the information needed to create a new message instance.

  1. go to /admin/config/workflow/rules
  2. click Add action, in the React on event selection list, select After saving new content event. (you need to add another event After updating existing content as well)
  3. Restrict this rule to fire only to the blog post content type by using Content is of type in the condition section
  4. Create a new action, select Create a new entity from the Select the action to add selection list. Then select Message from ENTITY TYPE selection list.
  5. Select the blog message type that we created in step 1 from the TYPE selection list.
  6. Type node:author in text field of the USER section
  7. Do not change anything else, click Save.

The above steps creates a rule that triggers after every time a user creates a new blog post content type, the rules module will create a new message entity instance, to associate with that event. The next logical step is to provide data to the newly created message instance.

  1. click Add action again, select Set a data value from select the action to add selection list.
  2. Use data selector in the DATA section to target the property you want to provide data to
  3. Use data selector in the VALUE section to select the source of data for the above targeted property
  4. As a reference, I passed node to entity-created:field-blog-reference and entity-created:timestamp to entity-created:field-post-date
  5. Until you covered all the data needed in your message then click Save
  6. Repeat

Step 3: Create a block to display logged messages

In this step we are going to use the views module to create a block which lists all the blog post related activities of a given user id:

  1. go to /admin/structure/views
  2. click Add new view link and give a name
    1. in the Show section, make sure select Message, and leave of type to All
    2. check Create a block and uncheck Create a page
    3. check Use a pager as well, and set items to 5 per page, then click Continue & edit
  3. change Show: Fields in the FORMAT section to Rendered entity instead, I choose Full in View mode selection list
  4. Click Advanced button to expand it
    1. click Add button beside CONTEXTURE FILTERS
    2. In the Search text field type uid, check Message: User uid in the result list
    3. in the next screen select Display contents of "No results found" in the WHEN THE FILTER VALUE IS NOT AVAILABLE section and check Specify validation criteria, select Basic validation in Validator selection list, and select Display contents of "No results found" again in the Display contents of "No results found" selection list

That’s it. Congratulations! You have done it!

Web Development

Would you like to know more about what we do?

View Our Services

Graphic Design Cheeky Monkey

Have a look at some our client work

View Our Work

Cheeky Monkey Discovery

Learn more about Cheeky Monkey Media

About Us

Comments