Get files() of $page that come from other pages

Here in the Docs for File Fields is described how to query other files from other pages.

Now when I select a file from another page in my file field (which therefore resides in another folder) I cannot access this file if I call the $page->files() method.

What I would expect is to be able to get all the files that are being used in this page, or am I missing something?

Is there another way to achieve that?

(What I actually wanna achieve is, to display in the Panel in my Files Section in a separate column where the individual files are being referenced, since I have a global Files section and most of my File Fields have some sort of query added to make sure all files that are linked multiple times within the same page aren’t actually duplicates but can be replaced with one single click in the Files Section. (that sounds really complicated if I read that…:thinking:))

1 Like

If you select files in a files field, you will need to call the specific field and apply the ->toFiles() method to it, e.g.

$page->yourFilesFileName()->toFiles()

Keep in mind that existing method names are reserved, to if you name your files field files you cannot access it via $page->files() as this is an existing method. Either choose a different name for your field or access it via $page->content()->get('files')->toFiles().

You lost me a bit with that last part, I couldn’t quite follow what you are trying to achieve.

Sorry for not being very clear…
The issue is, that I don’t have a specific page that I can query from.

My panel is structured in a way that the files that can be selected for file fields are all stored in one folder, to be able to make sure there are no duplicate files in various different pages. Files are being referenced on different pages, therefore my client doesn’t wanna upload the files on every page again and again, if a file needs updating.
I achieve that by adding a query to the file field and setting the uploads path:

fields:
  document:
    type: files
    query: site.index.files.filterBy('type', 'document')
    uploads:
      parent: page('document-page')

Then in the Files Section of my Panel page, which is another tab, I’m setting the parent to the same document-page as referenced above:

sections:
  someFilesSectionName:
    type: files
    parent: page('document-page')

(I do that by the way in this complicated way because I’m still on Kirby 3 which doesn’t have the query option for files sections yet…)


Now what I want to achieve is to display the page names in the Files Section where each file if referenced on, to be able to give the user a nice overview over all the files like so:

For that I need to query the files that each page obviously holds. That’s kindof where the issue starts.

Currently I am taking all pages and then checking on the files that each page provides. But, as mentioned before, this only returns the files on a file system level, not the actual files belonging to the page model.

So what I was trying, which didn’t work as expected:

site()->index(true)->pluck('files')

Does that make a bit more sense, what I’m trying to achieve? Maybe it’s just not possible that easily, then I have to find some other workaround…

Thanks in advance!

Well, basically, for each file you would have to loop through all pages and check if it is uses in the document field. For that purpose, I would create a custom file method.

With a lot of files and querying the whole index multiple times, this can slow the Panel down considerably. So some sort of caching would make sense. I can remember there was a plugin related to this task, let me check.

But this works a bit differently, not as info in a section.

Wow, awesome, thank you so much!
This is exactly what I’m looking for.

And I thought I looked in plugins if there is something available…?!