Checkbox fields, default values and bulk updating

Hi all

I’ve added the following blueprint to my articles blueprint. I plan to use the checkboxes to choose which one of the options will be displayed on the articles pages. Basically saying if this value exists in options then display this…

side_blocks:
    label: Side Blocks
    type: checkboxes
    default:
      - social
      - radio_player
    options:
      social: Social Media Links
      radio_player: Radio Player
      magazin_recent: Magazin Recent Posts

The default values are not automatically checked, are they meant to be?

In order to add the values to the article, I have to edit every article, check the two values, and save the article to make the values checked. Is there a way to automate this?

It’s one of the drawbacks with a file-based system: migrations. But of course you can automate it, it’s just there’s probably no routine around it as with a database system. As it seems a good idea to have it, I created this Kirby roadmap suggestion which you can vote for, if you agree.

Right now, you will simply write a one-time PHP script with all your changes (fopen(), regex replace, file_put_contents/fwrite() that you execute via PHP-CLI once. You can also do this within Kirby and use their helper functions for it, e.g. $file->writeContent().

1 Like

Thanks for your reply. I have no idea how to do that :smiley: but with checkboxes, if I list default values, does that mean those values should be checked by default?

As far as I know the default is only used for when you create new pages.

I usually write migrations via a custom route. E.g. in your case:

[
  'pattern' => 'updatecheckboxes',
  'action'  => function () {
    $kirby = kirby();

    if(!$kirby->user()) {
      go('error', 404);
    }

    $kirby->impersonate('kirby');
    foreach($kirby->site()->index() as $p) {
      try {
        if ($p->side_blocks()->isEmpty()){
          $p->update([
            'side_blocks'=> 'social, radio_player'
          ]);

          echo $p->title() . ' has been updated<br>';
        }

      } catch(Exception $e) {
        echo $e->getMessage();
      }
    }

    die;
  }
]

PS: I’ld love a more formalized way of handling this. I have upvoted your notion request @helloanselm !

1 Like