Query subpages of selected page inside the same page

Hi, maybe someone can help.

I am trying to query subpages (products) of a page selected in a field (productsPage) on the same page

      # Select products page
      productsPage:
        label: 
          en: Select products page
          de: Seite Produkte auswählen
        type: pages
        query: site.index.filterBy('template', 'in', ['shop'])
        info: "{{ page.categories }}"
        subpages: false
        multiple: false
        empty: 
          en: No products page selected
          de: Keine Produktseite ausgewählt
      # Select products
      products:
        label: 
          en: Select products
          de: Produkte auswählen
        type: pages
        query: page.productsPage.toPages.children
        info: "{{ page.category }}"
        subpages: false
        empty: 
          en: No products selected
          de: Keine Produkte ausgewählt

The second query (products field) comes up empty.

Any help is highly appreciated.

Just so I understand correctly, you want the content of the products field to be dynamically populated depending of the page selected on the productsPage field?

Because if that’s the case then your issue is that the value is not populated every time you change the field. I suspect that if you pick a page, hit save and then reload the page you will see the correct subpages inside the products field.

Actually, I just tested and it seems like it’s working for me. But not untill I hit save.

Unfortunately, this is not possible on the fly, because your second field knows nothing of the first before the page is saved.

You would need a custom solution here, i.e. a field that listens to changing values of the first field. Or maybe a combination of two fields in one.

See also: Programmatically change the content of the file field - #38 by bastianallgeier

Hi, @manuelmoreale, unfortunately, it doesn’t work in my setup even if I save and then try again. That specific install has Kirby 3.7.3 version. But I would doubt it will behave differently in 3.8.

That “seems like it’s working” is tricky, I don’t know how to understand it :smiley:

Hm, @texnixe I’ll try. I can solve this differently but wanted to see if it is possible. It seemed like it could work.

When you mention Programmatically changing blueprint, I have one that doesn’t work in Kirby 3.8.2 but it worked in Kirby 3.7

<?php

$work = site()->children()->findBy('intendedTemplate', 'work');
$minwidth = $work->coverWidth()->isNotEmpty() ? $work->coverWidth()->toInt() : '600';
$minheight = $work->coverHeight()->isNotEmpty() ? $work->coverHeight()->toInt() : '800';
$ratio = $work->coverRatio()->isTrue() ? '' : 'fixed';

return [
    'type' => 'image-clip',
    'clip' => [
      'minwidth' => $minwidth,
      'minheight' => $minheight,
      'ratio' => $ratio,
    ],
];

This is the field in the Project template that listens to the parent Work template setting. It influences the cropping size of the Kirby3 Image Clip plugin. But, for some reason, not anymore. It always uses 600 x 800 no matter what I do. Also, it doesn’t listen to ration setting.

But, maybe what you suggested Programmatically change the content of the file field - #38 by bastianallgeier could be the way to go for both issues.

I just found a mention of this listen pattern and @bnomei has a similar use case here:

Might be worth checking out.

1 Like

Thank you, Sonja, I will spend some time on it.
Although, I tend to give up when it becomes too tricky/hacky, and find another angle and different solution. When tricky/hacky solutions like that pile-up and Kirby changes some core things, it is very difficult to debug and maintain. So, I am always aiming for the most simplistic Out-of-the-box-Kirby-supported solutions. It saved me an abundance of time so far. :smiley:

Haven’t tested 3.8.2 yet, but your example works fine for me in 3.8.1.1 (latest stable).

Hm, I will then create more independent tests. It didn’t work in an install I updated from 3.7.3, maybe adding page and file IDs influence it. I will create fresh pages.

If you didn’t delete your test, could you please check if changing options in the parent template work properly after you cropped the image the first time? Does the second crop changes the crop size? It seemed to me that whatever is selected first somehow stays permanently for some reason (probably plugin related).

Sorry, I deeply apologize :pray: My bad.

In my install there is a small difference that is actually big difference. I had this

$work = site()->findBy('intendedTemplate', 'work');

Which doesn’t work. I wanted to cover more than children, if page is lower in page hierarchy.

With
$work = site()->children()->findBy('intendedTemplate', 'work');
It works.

Very sorry :see_no_evil: