Drupal Menu Badges + Views + Flags + Rules = Counting Messages

The Drupal project I'm currently working with get's complicated each and every day - so for a start I faced a problem to display dynamic content as menu items - i found a great not to say one of the greate findings this years in field of Drupal development - Drupal  Menu Views , then I faced a problem to display tokens in Menu paths - again a superb solution with help of Menu Tokens module.

Finaly I faced another Drupal Menu related problem - to display a comment count in a Dynamical Menu with menu tokens enabled. 

I must admit - I was lucky enough to solve this problem as well - The Solution comes from another great Drupal module - Menu Badges

About Menu Badges

Provides a method for adding iOS-style badges to menu items. 

Adding Badges to Menus

Once enabled, go to Administration > Structure > Menus, and click "list links" next to the menu containing the target item. Click "edit" next to the item and select the badge to display with the Display Menu Badge select box.

Adding Badges to Tabs

For adding badges to menu tabs, there is a tab under Administration > Structure > Menus called "Tab Menu Badges". Search for the menu router path of the tab you want to alter, then select a badge from the corresponding select box.

Badge Types

The module includes five example badges, and new badges can be created easily with Views. See this screencast for instructions: http://www.youtube.com/watch?v=4AoZQNg5QOI. Modules can also supply their own badges with a hook implementation. See the README.txtfile for details.

 

This is what I got after a couple of minutes playing with views and Menu badges

Menu Badges Example

Menu Badges Example

Now - as you can see from the above screen - this small element actually takes a lot views and a lot custom CSS - but it does well what it must to do.

Now the tricky part was to setup a view - which actually counts items, to be displayed on Menu Badges.

I created a view with Menu badge display counting all comments published on site (this is the behaviour customer is looking for)

To get a count out of Drupal views - we must turn on Aaggregation and add a NID field and set it to count (use aggregation)

Menu Badge View

Menu Badge View

As you can see from screen above - I'm using Flags Relationship, to count kinda only those comment's logged in user hasn't read yet (the tricky part here - I have not yet figured out how to decrease counter). 

Flags are created by using another module: Drupal Flags. 

Nothing fancy there - download that module, create a new comment type flag (is_new), now to get things more complexed you will have to add Drupal Rules (again, there is Drupal Rules module, you can use).

I created a new rule - which basically does following - after comment is saved it's flagged as is_new (so I can use this flag in my views)

What I haven't yet figured out is how to create a rule, which removes flag is_new after user has viewed particular comment.

Anyway - we have fully functional views, flags and we have menu badges module.

What we do now - we go to our Menus and add a menu badge to links - menu badges are created from view we made before

Menu badges link

Menu badges link

That's all - you can use CSS to style your badge icon.

P.S. If you have some idea how I can create a Drupal rule, to remove is_new flag after comment has been viewed - please drop me a comment!

about

Hi! My name is Reinis Fischer (38), a proud dad and devoted husband. CEO and Founder of Terramatris crypto hedge fund, drone enthusiast, world traveler, photographer, and passionate lover of Georgian cuisine (vegetarian).

An ex-pat living in Georgia since 2011, I trade stocks, take photographs, work out at the gym, and many more. Here I write about travel, finance, and other things that might interest me.