Skip to content
● Meet Grav Guard: keep every Grav site you run secure and up to date, all from one place. Free for up to 3 sites. Read more →

Count all users, and sum up a custom field from each user

Solved by pamtbaau View solution

Started by Anna 6 years ago · 2 replies · 785 views
6 years ago

Hey everybody,

I thought this would be simple, but I'm actually stumped. I would like to – in a plugin – count how many users there are, and get the total sum of the contents of a custom field that each user has. So some have 10 or 30 or 200 written in there, and I'd like to print the total sum of that field from all the users somewhere.

My idea was to count all the yaml files in the accounts directory, but I'm not sure how to find that directory without hardcoding the path (could do that, in this case, but it's hacky). I tried $dir = $locator->findResource('user-accounts://', true); since that works for the data directory :upside_down_face: but that throws a Crikey. And I took a peek at the Admin Addon User Manager plugin, however I feel I won't understand that quickly enough…

So as always I am super grateful for your ideas! And meanwhile I'll see if my hacky solution would even work…!!

6 years ago Solution

@Netzhexe, Would

PHP
$dir = $this->grav['locator']->findResource('account://', true);

be ok for you?

See streams.

last edited 02/22/21 by pamtbaau
6 years ago

Oh absolutely! I made it work ok with getcwd() and adding /user/accounts to that – but your way is nicer. Thanks also for the link about streams! I still have a lot to read apparently ;-)

Suggested topics

Topic Participants Replies Views Activity
General · by themetman, 1 day ago
2 41 18 hours ago
General · by Hanns Mattes, 1 week ago
0 121 1 week ago
General · by Karmalakas, 1 week ago
2 185 1 week ago
General · by Hanns Mattes, 3 weeks ago
0 235 3 weeks ago
General · by Dima, 4 weeks ago
1 211 4 weeks ago