Accessing a certain page in custom form fields?


is it possible to get a page object inside of a custom form field?

I try to create a field where I want to fetch all children of a site and display them as SelectField items.

How can I get access to a certain page object?
I tried this:

class SponsorsField extends SelectField {
  static public $fieldname = 'sponsors';

  public function input() {

    $sponsors = pages('sponsors')->children();

    foreach($sponsors as $sponsor) {
      $select->append($this->option($sponsor, $sponsor, false));

$sponsors seems to be null and therefor the foreach loop fails.

I had a look at some related threads like Get the page object from a given field value but can’t get a solution out of this.

May I ask why you want to create a special field instead of using the query option in a select field?

    label: Sponsor
    type: select 
    options: query
      page: sponsors
      fetch: children
      value: '{{uid}}'
      text: '{{title}}'
1 Like

If there’s a special reason why you need a custom field: There’s a typo, it has to be page() instead of pages().

Is there any way to use the query options with a custom field other than extending a field that already has this option?

Doesn’t “page” refer to the page where I currently am? I want to find a page with the name “sponsors” and get its children. When I used page I didn’t get the right ones but the children of my current page.

This might be what I need. Never saw this in the kirby docs. But of course I don’t know them in and out like you do :wink: . Most of the time you got the feeling that the solution might already exist but you can’t find it by searching and then you try to build it because the client pays your hours and you need to make progress (and can’t search forever ^^).

Here is what I need to build, or at least I am trying ^^.
I want to create a page “sponsors” that has sub-pages. Every sub-page has “n” sponsors which are structured fields with an image, some description and a link.

Now I want my client to be able to fill in which sponsors he has (into the appropriate category) and then select one of those sponsors on different sub-pages. On those sub-pages the sponsors will be shown somewhere.

So he has to define the different categories once and he then knows that if he changes them in one place, all sponsors are correct in any other place where they appear.

To do this I thought I will build a select field where I fetch all sponsor children, let the client chose which sponsor block he wants and display the correct one. I want to be able to add a field on any page or sub-page to chose sponsors.

Either it is too late or I’m a bit dumb, I’m not sure what exactly you need to query:

  • just a subpage => that can be done with the query option
  • a structure field in a subpage => then you need a custom field

Edit: doesn’t it work if you pass the name of the page?

$sponsors = page('sponsors')->children();

If you look at the select field, I think this bit is where the magic happens:

public function options() {
    return FieldOptions::build($this);

So maybe adding that bit of code to some field where it makes sense might work, not tested though.

The FieldOptions class is defined in /panel/src/panel/form/fieldoptions.php

1 Like

I think I need some pictures to describe the issue better ^^

So this page “sponsoren” is kind of like a database. It is invisible and should just store data in one place to be accessible on other sub-pages so you don’t have to repeat the same data over and over again.

Inside of “sponsoren” you got sponsor categories that you can create and then add different sponsors to them.I used structured fields for that so you can add as many or as little sponsors that you got.

Inside of each sponsor (e.g. main sponsors) you got something like this. A site with a structured field which defines what data should be stored. You can add, delete and update sponsors like you want.

Now I want to access the categories of sponsors on ANY given page. For example I could be on a projects page and there I want to chose what kind of sponsors did support this event (main sponsors or sub sponsors, etc) so here I want to chose the category and then in the template I fetch from that defined category all the related sponsors. And this is where I am now and trying to build that Select Field.

You could also imagine this a bit like a tagging system but I want to limit the tags to the categories that exist and then I want to chose from them on every page individually.

Tricky to describe but I think this should give you some insights into the project ^^.

1 Like

Ok, thanks for clarifying, but then the blueprint query I posted above should work as expected, because all you want to select is one of the children of sponsors.

Yes I just tried it out :grin: and it works perfectly. You’re the best :heart_eyes:

Thank you so much for that solution. Sometimes you can’t see the forest cause of all those trees. Didn’t knew that select fields where that powerful. I never thought that they can do that by default :smiley:

Oh yes, they are, since Kirby 2.3.0 you can even query the field of another page :slight_smile:, not structure fields though.

This also works with radio buttons and checkboxes (in case you want to select multiple sponsor categories) and it’s great :slight_smile: