1

I am using the Message module in combination with the Flag module in order to build a simple notifications system for my site.

More specifically I have created a page in Views showing all (new) messages for the current user. The page is accessible through a menu link (called "Messages") in the front page. I have also created a message flag so users can flag/unflag there messages as new/old ones.

But instead of forcing the user to manually flag/unflag his messages, I would like all messages to automatically be unflagged (i.e. become old) when the user visits the messages page (or a certain context in general).

Any hints on how to achieve this?

1

This sounds like a job for Javascript

Flags has an option to allow the link to use AJAX. So I would dig into the Javascript that the flags modules uses and then create your own script that walks over and triggers each link when the page is loaded.

0

Although the previous answer seems correct i managed to do this in another way avoiding research, coding and other stuff.

I used Rules, Rules Bonus pack and Panels to do this. Rules bonus pack provides the event "When a page variant is rendered" to trigger actions. So what i did was to create a new page in panels, place my (page) view there and then created a rule that whenever the page is rendered to unflag all the messages.

0

Below are some details about the steps to be completed, specific to your question here ...

Step 1:

Create a VBO view of your unread messages, similar to what is described in my answer to "How to allow users to manage their own Message Stack messages?".

Step 2:

Create a custom Rules Component that will execute the VBO list of unread Messages, and which looks like so:

{ "rules_mark_all_unread_messages_read" : {
    "LABEL" : "Mark all unread messages read",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules", "views_bulk_operations" ],
    "ON" : { "init" : [] },
    "IF" : [
      { "user_has_role" : {
          "account" : [ "site:current-user" ],
          "roles" : { "value" : { "2" : "2" } }
        }
      },
      { "text_matches" : {
          "text" : [ "site:current-page:url" ],
          "match" : "user\/\\d+\/messages\/unread",
          "operation" : "regex"
        }
      }
    ],
    "DO" : [
      { "views_bulk_operations_action_load_list" : {
          "USING" : { "view" : "messages_by_user|page_3" },
          "PROVIDE" : { "entity_list" : { "unread_msgs_list" : "A list of unread messages" } }
        }
      },
      { "LOOP" : {
          "USING" : { "list" : [ "unread-msgs-list" ] },
          "ITEM" : { "current_msg" : "Current message" },
          "DO" : [
            { "component_rules_flag_a_message" : { "message_to_flag" : [ "current-msg" ] } },
            { "drupal_message" : { "message" : "The message with ID [current-msg:mid] (created [current-msg:timestamp]) was marked as read." } }
          ]
        }
      }
    ]
  }
}

Some more details about the rule above:

  • Rules Event: Drupal is initializing.
  • Rules Conditions:

    • only for authenticated users.
    • check if the path being visited is user/%/messages/unread (adapt this path if you use another path).
  • Rules Actions:

    • Create a list of unflagged messages.
    • Loop over each of the unflagged messages found, and flag those messages.

This rule also performs an extra "set message" action, so that you can see what it does if you want to try it in your own site (it shows a message (oeps!) for each message being flagged). Remove that message after QA-testing if you want.

That's it, works like a charm!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.