Filter by number of pages in pages field

Hi,
I am using the stats section to display some information about users. I have this currently as the value:

{{ kirby.users.filterBy('role','entrant').count }}

Which is great. I now need to apply an additional filter, to show the count of users who have 9 pages selected in a pages field called progressDetails in their blueprint. I have tried this but I assume I am getting something wrong:

{{ kirby.users.filterBy('role','entrant').filterBy('progressDetails.toPages.count','==',9).count }}

If anyone has any pointers how to achieve this that would be super.
Thanks!

filterBy only accepts a field or method as first parameter, so you cannot do any field modifications here. If I were you, I’d create a custom users method that returns what you want.

Hi,

Thanks for the pointer. I have set up a users method but can’t seem to get it to work. This is what I have (I am just checking if the field is empty for now as a test):

Kirby::plugin('my/plugin', [
    'usersMethods' => [
      'startedTrail' => function () {
        return $this->progressDetails()->toPages()->isNotEmpty();
      }
    ]
]);

That is in site/plugins/users-methods/index.php

And this in my stats section:

value: "{{ kirby.users.startedTrail.count }}"

But nothing is being shown in the section for that stat

Your method returns a boolean value, which you cannot count. What you want is to return the pages collection:

Kirby::plugin('my/plugin', [
    'usersMethods' => [
      'startedTrail' => function () {
        return $this->progressDetails()->toPages();
      }
    ]
]);

Thanks for your help, sorry, it must be a case of the Mondays but I am struggling with this.

I am back to trying to get the number of users with 9 pages selected in the progressDetails field in their blueprint. I have this:

site/plugins/users-methods/index.php

<?php

Kirby::plugin('my/plugin', [
  'usersMethods' => [
    'completedTrail' => function () {
      return $this->filterBy('role','entrant')->progressDetails()->toPages()->count() == 9;
    },
  ]
]);

and this:

site/blueprints/site.yml

stats:
  type: stats
  size: huge
  reports:
  - label: Entrants
    value: "{{ kirby.users.filterBy('role','entrant').count }}"
  - label: Completed trail (found all 9)
    value: "{{ kirby.users.completedTrail }}"

There should be a figure of 16 in the stat, but it is just blank (not even showing 0)

This here returns a number

But this here returns a boolean value, whereas you have to return a filtered collection

 'completedTrail' => function () {
      return $this->filterBy('role','entrant')->filter(fn ($user) => $user->progressDetails()->toPages()->count() === 9);
    },

This will now return all users with role entrant which have 9 pages in there progressdetails field.

Hi @texnixe ,

After a good night’s sleep I can see where I was going wrong!
Thanks for your patience