0

Is it possible to create a view be that a page or block that shows the following.

-Total number of nodes user has created. -Total number of comments (all comment types) user has posted. -Total of how many times this user nodes or comments have beed Flaged as HELPFUL. (flag module is used for this)

All this using the path /user/%user/stats

Which means contextual filter user ID value will be provided from url path 2.

I am stuck with the three bullet points above. Or how to calculate the totals for each.

4
  • You can make a route for the above, then create different Views blocks for that route. Alternatively, you could do your own querying and return in the controller for that route if provided.
    – Kevin
    Oct 17, 2018 at 22:21
  • Thanks Kevin. What's a route? Hahaha....but yes really.
    – Progonat
    Oct 18, 2018 at 1:37
  • 1
    I think you will find it faster to code a route in a custom module, and either place blocks (made with various Views) on it, or create your own blocks that query and return data. Would be a good lesson in development.
    – Kevin
    Oct 18, 2018 at 2:29
  • I also recommend to write the queries by yourself. From complexity perspective the are pretty straightforward.
    – ssibal
    Oct 18, 2018 at 7:25

1 Answer 1

1

You can use Views Aggregation to count the number of results for your specific cases.

Count number of nodes authored by a user

Since your results (to be counted) are nodes it makes sense to create Content type Views:

  1. Create Content Views with Page Display and Filter out the node types you want to count later.
  2. Add node Title field or Node ID under Fields to test you have the correct list of content. Add Author ID or Name to test the Contextual filter later.
  3. Set Path to user/%user/stats, set Menu Tab so it displays under Drupal tabs on user pages
  4. Add a Contextual filter Author UID (provide default value; User ID from URL) and test by entering user ID number into the Preview, make sure you see all the nodes created by the author with that UID
  5. Under Fields make sure you only have ONE node field (if you have both NID and Title, remove Title). You can keep the Author fields, for testing if nothing else. We'll use one unique node field to count them aggregated. If we add another node field it will force Views results to show one node per Views row, which will make counting impossible
  6. Remove Sorting criteria - they mess up Aggregation!
  7. Turn on Aggregation
  8. Edit the NID Field Aggregation setting to Count or Count DISTINCT (if you don't have duplicates in your Views results Count will do)

You now have Views counting the results! If you enter user UID into Preview you will see the number of their nodes in the NID field (adjust the NID field label accordingly).

If you enter all into Preview you will see a list of all Authors (from that nodes list, if you kept that Field) and the number of the nodes they created.

If you visit a user page you should see the stats tab added to Drupal tabs. Adjust the Permissions for the Views Page to limit who can see it.

Count number of comments authored by a user

Repeat the above procedure to count comments, only start with Commment Views Type, set a different Path user/%user/stats-comment and Menu tab

Count flags

To count how many of the listed nodes are flagged you'll need a Relationship to that information, then you'll be able to list a Field with the Flag status and use Aggregation to count them, similar to the above.

There are answers/articles on how to set up Flag Relationship in Views to display various information.

Your Answer

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

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