Panel: Querying the parent’s structure field to populate a checkbox field

Hey,
I’m new to Kirby (& PHP). My problem:

I want to query the parent’s structure field to populate a checkboxes field. The tags solution (Option from other field / source field) in the docs won’t work in my case.

This solution by @rhawbert and @texnixe with a select field works great.

However, in my case I need the ability to assign more than one category to a project (multiple selection). Because of that I tried to get this example to work with a checkboxes field. Can someone push me in the right direction? How do I have to alter the following customcheckboxes.php?

<?php
class CustomcheckboxesField extends RadioField {

  public function input() {
    $value = func_get_arg(0);
    $input = parent::input($value);
    $input->replaceClass('radio', 'checkbox');
    $input->attr(array(
      'name'     => $this->name() . '[]',
      'type'     => 'checkbox',
      'value'    => $value,
      'checked'  => ($this->value === 'all') ? true : in_array($value, (array)$this->value()),
      'required' => false,
    ));

    return $input;
  }

  public function value() {
    $value = InputListField::value();

    if(is_array($value)) {
      return $value;
    } else {
      return str::split($value, ',');
    }
  }

  public function result() {
    $result = parent::result();
    return is_array($result) ? implode(', ', $result) : '';
  }

  public function item($value, $text) {
    $item = parent::item($value, $text);
    $item->replaceClass('input-with-radio', 'input-with-checkbox');
    return $item;
  }
}

Maybe @texnixe or @rhawbert can help me? Thanks.

Hi @steffen and welcome to the Kirby community :slight_smile:

You have two options:

  • leave the checkboxes field as is and use the query API, for a way to create a json file in /assets, check out this post (btw. the problem mentioned with localhost has been solved in the meantime)
  • extend the current checkboxes field, I’ll try to look into this, haven’t done it with that field yet

@textnixe Which link?

In case of solution 1: How do I fetch the data into a json file and save it in the assets folder? Can you point me to a resource where I can read how to do that?

Is the hook approach in site/config/config.php below properly?

kirby()->hook('panel.page.create', function($page) {
  // How to create the json file and
  // how to save it in the assets?
});

Sorry, I probably took the wrong hook:

kirby()->hook('panel.page.update', function($page) {
  // How to create the json file and
  // how to save it in the assets?
});

Oh, sorry, here is the link: Panel: querying "site.txt" structure fields as pages/subpages fields' values

Thank you for the quick support @texnixe.
Works like a charm. You’re a lifesaver. :slight_smile:

One thing though, is there a way to define the path of the json file in the blueprint as a relative one? (So that it don’t have to be redefined every time the page is moved to a new domain ? …)

Hi @steffen, no, unfortunately that’s not possible, because a real URL is expected.

Hey, I’m now trying to extend the checkbox field as well. I managed to get a fully dynamic select dropdown, but now I need to be able to have a dynamic field to auto populate from another fields structure.

Here is how my structureselect looks currently, I’d like basically the same thing, but I am not sure how to convert that over to extend the checkbox field.