Files section / field: multiple parents

#1

Can you combine query results? For instance: query more than one parent in a files field or section?
Concrete use case: I need a files field where the user can insert an image from either one of two folders. It would be nice if I could also display all the images from these two folders in a files section.

0 Likes

#2

A files section can only have a single parent. To display files from multiple parents, you would have to create multiple files sections or a custom section.

Querying multiple pages in a files field should be possible.

0 Likes

#3

EDIT: had a mistake in my query

Thanks for the suggestions. Multiple file sections is the workaround I have right now. But I don’t see how I can combine queries in a files field. I tried

  fields:
    image:
      label: Image
      type: files
      query: page.images.add(site.find("images").images)

(And also extend instead of add)

Note: there is an images folder at site level. Both page.images and site.find("images").images queries work fine, but I don’t see how I can combine the results.

0 Likes

#4

You could probably use a files collection?

0 Likes

#5

Collections don’t seem to work in queries? Tried:

in collections/images.php:

return function ($site) {
  return $site->find("images")->images();
};

and in the files field:
query: collection("images")

but that yields “The files query does not seem to be correct”

0 Likes

#6

Try

query: kirby.collection("images")
0 Likes

#7

Of course. That works indeed. But now I’m back to square one because the $page object is not available in custom collections, nor does $site->page() seem to work there. So I’m still not able to collect site files with page files.

Guess I’m asking too much :slight_smile:

0 Likes

#8

But you can combine the collections, for example:

<?php
return function ($site) {
    $siteImages = $site->images();
    $pageImages = page('photography')->children()->first()->images();
    $allImages = $siteImages->add($pageImages);
  return $allImages;
};

You can use the page() helper here to get a page by ID, or use $site->find() or whatever you need to find the page or pages you want to query for files.

0 Likes

#9

Yeah, but I’m querying the collection on a files field of a page I’m creating.

The catch is: I want query the files from this page (that I’m creating) + some other files I can get via the $site object.
So I can either query the $page object directly in the query but then I can’t combine the results with whatever else I want to query (apparently), OR I can use a custom collection and combine stuff there but that collection is agnostic of the page I’m creating so it can’t add that page’s files there either.

0 Likes

#10
return function ($site) {
    $siteImages = $site->images();
    $pageImages = page()->images();
    $allImages = $siteImages->add($pageImages);
  return $allImages;
};

The page() helper without a parameter always refers to the current page…

So if you query that collection in Page A, you will get the site’s file + the images uploaded to Page A.

0 Likes

#11

I did try the $site->page() helper, but the list turns up empty if I do. So I presumed it doesn’t work from a custom collection. Do you think that’s a bug? If so I can file an issue.

0 Likes

#12

Yes, you are right, I tested in the home page and there it works, but not on other pages. So it doesn’t seem to be possible to fetch the current page in a collection, because the page helper in that case always gets the home page, not the current page, too bad. $site->index()->current() doesn’t work, either.

The page helper works fine when calling the above collection in any template, but in a Panel context, it stops working and defaults to the home page.

:cry:

0 Likes