Limit "featured" articles / posts to a max number

Hi all,

Not sure if this is possible within Kirby but still wanted to check:

On a page I did for a client, there is a “featured” toggle in the panel, which determines if the project gets shown on the homepage or not. The homepage is designed to feature a certain max. number of projects.

My question is: Would it be possible to “let the panel know” that there is a max number of projects that can activate this toggle? Would it be possible to prevent editors from activating it if the max number is already reached and maybe even display an error?

Currently, I’m using “->limit(number)” in the frontend which does the trick for now but isn’t quite the same and might create potential issues in the future when there are e.g. more than the intended max number of projects with “featured: true”.

Grateful for any help!

Thank you so much

I can think of three ways to achieve something like this:

  1. You keep track of the number of featured projects in site.txt or the parent page (using a page.update:after hook that increments this counter whenever a new projects is featured or decrement when unfeatured) and create your blueprint programmatically. Then you can decide to show the toggle based on the stored value.
  1. You use a page.update:after hook to throw an error when the max number is reached. That won’t hide the toggle but user cannot feature more projects. Additionally, you could show help text that tells them that the max number has already been reached.

  2. You extend the toggle field so that you can set the disabled property via query language (don’t know how much work that would be)

Thanks @texnixe! I like the second approach, however, I am not quite sure how to build the hook. Do you have any pointers on how to tell the function to update a field in site.txt? Sorry, I am quite new to php and kirby (usually working in JS country) :slight_smile:

This is what I have currently in the config.php:

 'hooks' => [
        'page.update:after' => function ($newPage, $oldPage) {
            if ($newPage->featured() === 'true') { // not yet sure if === works in php :D
                // update site.txt here
            }
        }
    ]

Grateful for any pointers. :slight_smile:

You probably mean the first approach not the second?

To update a counterfield(adapt to what you field is named), depending

  'hooks' => [
        'page.update:after' => function ($newPage, $oldPage) {
   
            // Get featured value from both old and new page
            $newPageFeatured = $newPage->featured()->toBool();
            $oldPageFeatured = $oldPage->featured()->toBool();
            // we only want to do something if the value has changed
            if ($newPagesFeatured !== $oldFeatured) {
                // do we want to increment or decrement?
                $method = $newPageFeatured === true ? 'increment' : 'decrement';
                $site->$method('counterfield', 1);
            }
                    
        },
   ],