A person can log multiple cases: Case 1, Case 2, Case 3

a case can have multiple states: New, Active, Resolved

Using a views table I want to generate a view where each person has one row, and I can see how many new, Active and Resolved cases each has:

Person   | States 
Person 1 | New(1), Active(3), Resolved (13) 
Person 2 | New(5), Active(3), Resolved (1)

Or something like this:

Person   | New | Active | Resolved
Person 1 | 1   | 3      | 13
Person 2 | 5   | 3      | 1

With normal aggregation all I can achieve is:

Person   | Cases
Person 1 | 17
Person 2 | 9

Surely I am missing something?

  • I think the states would need to each be stored as a separate field for this to work without using a custom template. There's no way (that I know of) to tell Views to return a row only if the value == x. But you could definitely put that logic in a views template. The problem with that is you're altering data at the theme layer. So the best option would be to use a custom module or to write a new display plugin. See this answer for help with the module. Aug 5 '13 at 16:04
  • Thanks, that other answer is similar but does not make use of aggregation, so I would want a count per role (which does not make sense for that answer at all as it is per user and a user can only belong to a role once)
    – Duncanmoo
    Aug 6 '13 at 9:06
  • Cases are nodes and States are options of a list field? Aug 7 '13 at 10:22

Views_merge_rows module will save your day! Assuming that the States field is a list field.

1) In your aggregation enabled views with table display add a User:Name (Group results together), a States field (COUNT, Excluded from display) and again a States field (Group results together, Rewrite Results > Rewrite the output of this field: [field_states] ([field_states_1])).

So you will get a list of users with states but with one row per node. Here is a screenshot:

Views results unmerged

2) Then, enable the views_merge_rows module. In you Merge rows settings do this:

enter image description here

So you get what you want!

enter image description here

  • Thank you, I did not know about that module, and I see it is really new. Thanks for the clear instructions I am sure others will appreciate too.
    – Duncanmoo
    Aug 12 '13 at 14:23

You can achieve this by using the Views Field View module. More specifically

  1. Install and enable the above module.

  2. Create three (3) different views (of users) one for each state (for example new_state_view, active_state_view & resolved_state_view).

  3. At each view firstly remove any default Sort Criteria (and don't add anything in that section). Then use aggregation to count the states for every user. Don't forget to use a "user:uid" contextual filter in every view and to filter them appropriately for each state. Each view will provide you the number of states for every user.

  4. Create another user view, add "user:uid" as field and exclude from display. Then add a field of "user name" and finally add three (3) consecutive global view fields. Note: it is very important to keep the "user:uid" field before the global view fields.

  5. In the settings of each global view field choose the corresponding _state_view and display of that view (master display is fine). Last but not least use the [!uid] token (from replacement patterns: [!uid] == User: Uid (raw)) in the "Contextual Filters field" of those settings.

After the above you will have a view displaying a table of user names and next to them the counts of each state. You can theme the global view fields (inside views) accoridingly to present them the way you like.

  • I wish the guy who downvoted the answer could explain the reason.
    – Billy
    Aug 10 '13 at 15:06
  • Thanks for the answer Billy, it is not wrong but this method is very intensive as a table of 40 records would require 40 x n separate views to run where n is the number of states a row could have. This is why on the module page it says "It's highly recommended to use this module in conjunction with views caching."
    – Duncanmoo
    Aug 12 '13 at 14:35

You could add three custom PHP fields to your view using https://drupal.org/project/views_php, one for each case. Within each field's code you would then load the number of corresponding cases.

  • Could you elaborate on your answer? I have looked at views_php and find it hard to fathom.
    – Duncanmoo
    Aug 6 '13 at 9:08
  • I'm not sure what your custom function would be, but whatever it is, you should be able to add a field with custom PHP code to your view once the module is enabled. Let me know if you can't figure it out and I'll look at it again.
    – deinqwertz
    Aug 6 '13 at 18:46

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.