Custom values in a checkboxes field

This post is based on this topic https://forum.getkirby.com/t/show-other-pages-as-checkboxes/4089/7.

For many cases you can just add some options to the checkboxes field but blueprints does not support logic but there is a way to populate it.

To use custom values in a checkboxes field is really simple:

  1. Create a field or register a field as a plugin.
  2. Add the code below in the field file.
<?php
class CategoriesField extends CheckboxesField {
  public function options() {
    $categories = array(
      'value1' => 'Value 1',
      'value2' => 'Value 2'
    );
    return $categories;
  }
}

From a database

Iā€™m currently playing around with a database. Hopefully it will end up with a new site at some point.

I needed to populate the checkboxes field with data from the database. For me it looks like this:

<?php
class CategoriesField extends CheckboxesField {
  public function options() {
    $sql = query::getCategoriesAll();
    $results = db::query($sql);

    foreach( $results as $data ) {
      $categories[$data->category_name] = $data->category_title;
    }
    asort($categories);
    return $categories;
  }
}