Page field settings

Hi together is it possible to customize the page field(http://getkirby.com/docs/cheatsheet/panel-fields/page) ?
I want only to show pages of a subfolder instead of all pages of the site… any ideas?

I don’t think it works with the page form field atm, but you could go for a select field with dynamic options if that’s working for you: http://getkirby.com/docs/cheatsheet/panel-fields/select

default is the name of my subdirectory?

from the doc

you can use:

option: children

or

option: visibleChildren

Good luck!

Okay doesnt work for my situation, I need the children of another subpage… With options: pages , it works but I searched for a list which shows me only the children of a special directory. So the customer cant choose wrong pages…

Just read a lil further on the referenced doc page:

    label: Category
    type: select
    options: query
    query: 
      page: THEOTHERSUBPAGE
      fetch: children
      value: '{{uid}}'
      text: '{{title}}'

thanks mate, my fault … used the wrong spelling

Can we still try to make this work using a custom page field? I prefer a proper autocompletion page field to a huge select field that pops down.

It shouldn’t be that difficult right? In the code for a page field, the input() method has a 'url' field, that we can perhaps rewrite?

public function input() {

    $input = parent::input();
    $input->data(array(
      'field' => 'autocomplete',
      'url'   => panel()->urls()->api() . '/autocomplete/uris' //How to rewrite this?
    ));

    return $input;

  }

Yes, this is indeed a problem! I’ve been trying to fix it too. I can’t see my users scrolling through 50+ pages in a selection menu :confused:

The URL panel()->urls()->api() . '/autocomplete/uris' points to this method. It doesn’t currently support any filtering and will return every page.

You can however register a route from your own field and your route can return a filtered set.

I see.

For this routing solution, can you give an example of an approach? I tried a bit but I am confused of how to “connect” the route to the custom pages field, so that it reads from the custom pages field and returns the filtered set.

Sure!

You can create a new field in site/fields/subpage/subpage.php with the following content:

<?php

class SubpageField extends PageField {

  public function input() {

    $input = parent::input();
    $input->data(array(
      'field' => 'autocomplete',
      'url'   => purl($this->model, 'field/' . $this->name . '/subpage/autocomplete/' . $this->parent())
    ));

    return $input;

  }
  
  public function routes() {

    return array(
      array(
        'pattern' => 'autocomplete/(:all)',
        'method'  => 'post',
        'action'  => function($parent) {
          $ids = page($parent)->children()->pluck('id');
          
          return response::json(array('data' => $ids));
        }
      )
    );

  }

}

You can then use this in your blueprint:

page:
  label: Page
  type: subpage
  parent: my/parent/page

Let me know if that works.


I have created a feature request issue on GitHub about integrating something like this into the core page field.

Thanks for your solution. It does filter my input on the parent folder, so that it only gives autocompletion suggestions when I start typing the name of that parent.

But what I would like is to be able to have autocompletion for the children in that parent folder. So let’s say I have a content structure like this, where I filter for pages in the projects folder:

content/projects/lorem
content/projects/ipsum
content/projects/dolor

Then I would like to be able to type “lor” to see the autocompletion lorem, instead of typing “projects/lor”.

You could try setting default: projects/ so that projects is already typed in (not tested);
Or customize the code from @lukasbestle

Easiest way would be to replace pluck('id') with pluck('uid') in my code, but please note that only the UID will be stored then, so you need to use page('projects/' . $page->mypage()) in your template to get the parent back.

Wonderful! Thanks a lot, this is just what was needed! It will be super useful to put this in some way in the next Kirby update, or just have a plugin :slight_smile:

As I wrote, I added this as a feature request. We can’t promise if that will be in the next release though. :slight_smile:

This is good for my actual project so thanks you !

Though it’s not working when the subpage field is inside a structure field.

Any idea why ?

Works for me in a structure field. What exactly does not work? No autocompletion?

Exactly, and just for info, I also change the code for " pluck(‘uid’) " like it’s indicate here, but this is not where the problem come from.