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

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…!!

@Netzhexe, Would

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

be ok for you?

See streams.

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 :wink: