1

With reference to my previous question which was a little confusing or I should say it was mixed up with two different problems. I am posting my question again with clarification.

I have created a view, which shows activity logged by Message module. I then created a menu badge using menu badges module which shows the numbers on the menu item. - In start, when there is no activity, it shows 0, After there is new activity, say 2 new activities, the number changes to 2, which is correct, but when the page is viewed the count remains the same and does not decrease or disappear i.e 2, which is wrong. This is the problem. When the page is viewed, the number must disappear until and unless there is new activity.

Currently I am using Message:Message ID in the field to count. I have 3 different message types and I am showing it in one view. Actually, I am trying to create a notifications system. When, the user you are following does some activity, you will be notified.

Coming back to issue. For example, the menu badges module comes with a default badge for the private messages module (it allows users to send/receive messages to/from each other). It is working perfectly as the badge disappears when the users reads all the private messages and it reduces as well. Private messages module does not provide a view so i cannot see how it is working with the menu badges module.

Extra clarifications:

  • if the user visited the page containing the view that shows 2 rows corresponding to those 2 new activities, then both of those 2 new activities should no longer be included in the counter for its menu badge.
  • I wont use a pager. All items will be listed on the same page.
  • We only want menu badges to show count of new activities which the user haven't seen yet i.e the user haven't visited the page containing the activities-logs/notifications. Simply, when the user visits the page, the menu badges count must disappear.
  • Visiting the page means user has seen the notifications/new activities and now the count should not be visible as there is no new activity. As it happens on stackexchange websites: when you click the notification, the count disappears.
8
  • Can you refine your question to explain the "but when the page is viewed"-part of it: which page do you mean, and what do you have in place to view those messages? Or do you mean "if the user visited the page containing the view that shows 2 rows corresponding to those 2 new activities, then both of those 2 new activities should no longer be included in the counter for its menu badge"? Commented Aug 9, 2016 at 19:09
  • exactly what you said in the latter part of your comments above. The "Or do you mean "if the user visited and so on" part. The page which is showing the activities.
    – Umair
    Commented Aug 10, 2016 at 8:14
  • "Understanding a question is often 50% of the answer already ...", right? But what if a user would have dozens (instead of just 2) of such messages, so that the views result would be split in multiple pages (using a pager, say with 15 messages at maximum on a single page)? May I assume like "too bad, don't include any of those anymore in the menu badges counter, so also not the ones on a subsequent page which the user may never have looked at? Commented Aug 10, 2016 at 8:26
  • I wont use a pager. All items will be listed on the same page. Secondly, we only want menu badges to show count of new activities which the user haven't seen yet i.e the user haven't visited the page containing the activities-logs/notifications. Simply, when the user visits the page, the menu badges count must disappear.
    – Umair
    Commented Aug 10, 2016 at 9:57
  • visiting the page means user has seen the notifications/new activities and now the count should not be visible as there is no new activity. As it happens on stackexchange website. When you see/click the notification, the count disappears.
    – Umair
    Commented Aug 10, 2016 at 10:01

2 Answers 2

2

To make the Menu Badges counter disappear when visiting the page at notifications/new (as in your question), is something you can do by using a special feature of the Rules module, i.e. a technique that is similar to what is described in the answer to "How to execute VBO list of nodes in Rules on After updating an existing user account event?". Read on for more 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!

Note: this solution can also be used in combination with the solution detailed in the related answer. So maybe you want to set a special permission if you want to combine both solutions, so that admins only can mark selected messages as read or unread, if needed.

5
  • sorry for late reply. This is working perfectly. You are a genius pierre. Thank you for making an effort and taking interest in this question. Let me tell you guys, pierre is not only smart, but he is also a nice person. He takes a question seriously.
    – Umair
    Commented Sep 3, 2016 at 14:27
  • However, there is a slight problem. When I visit the view, in the admin area, the menu badge disappears. It is supposed to disappear when I visit the link and not the view itself.
    – Umair
    Commented Sep 3, 2016 at 14:31
  • @Umair : merci for the feedback. What is the URL related to your "when I visit the view in admin area"? Assuming it is not the one as in the (2nd) rules condition, you may want to try adding a 3rd rules condition like "and the URL is NOT the one in the admin area". Commented Sep 4, 2016 at 7:05
  • Can I make VBO work with non-entity?
    – Umair
    Commented Oct 11, 2016 at 1:40
  • @Umair not that I know. Commented Oct 11, 2016 at 5:32
0

This is what i have done, very easy and works, menu badges combined with flags & rules, make a NON global flag: node is viewed, make a rule: react on node is being viewed, flag your node with current user (can be many content types), in your badges view and relationship to flag, with current user, filter nodes by not being flagged, done.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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