Fetching page / file information into a block

Good day everyone, new day new question :slight_smile:

My client wishes a centralized file folder, this way I made a “Datenbank” folder, each file there it has a page with database.file template. The page structure looks like this (Folder name and template)

- ...
- 04-Unsere Schule
- 05-Downloads
- Datenbank (database)
┕ Schulanmeldung (database.file)
┕ Datenschutzblatt (database.file)
┕ Stundenplan (database.file)
┕ ...

The database template has a Kirby 3.7 introduced table page layout and nothing more, it works excellent.

In Datenbank only subpages with template database.file can be added. Every such file has an identification code in a field, it looks like this:

documentIdentifierCode:
  label: Dokument ID
  type:  text

Now, the idea is to “call” this file with information like title, download URL etc. via a custom block, I made this fieldset for that block, that looks like this:

 card.file:
    name: Dokument ID
    fields:
      transfercode:
        label: Dokument ID
        type: text

The block snippet looks like this:

<?php
$transfercode = $block->transfercode();
$items = page('Datenbank')->children()->filterBy('template', 'database.file')->filterBy('documentIdentifierCode', '==', $transfercode);
foreach($items as $item): ?>
<?= $item()->title() ?>
...
<?php endforeach ?>

Unfortunately it doesn’t display anything when using the stuff i wrote above. Can you please help me troubleshoot this issue.

Another thing, would it be possible to show inside a blueprint the title of file being called? More exactly here instead of text “Dateiabruf”:

Why are you talking about files here. These are pages, not files? So where exactly is the documentIdentifierCode field used?

Hi Sonja, indeed I didn’t make myself clear in first post :slight_smile:

Those are indeed pages, every page has exactly one file and information about that file. This way every file has it own separate page with basic information, the page can be opened in browser, printed and saved to a paper folder (I know, it’s not my idea but clients requirement).

This is how the database.file looks like in panel:

What I wanted is to add information from single database.file pages to another separate pages, visible for public (for example on Download page). Those infos would than be download url, file name etc.

My idea was to do it via block snippet I mentioned in first post and by reffering to it via Dokument ID, which is stored in field documentIdentifierCode in database.file.yml blueprint.

If you think, there is another better way to implement this, I’m very open for suggestions! :+1:

Hm, this should actually work, at least if such a template exists, but the filter by template is superfluous anyway if the database page can only have children with that template.

If there is only ever one page with that document id, I’d use findBy instead of filter, which should return a single page.

Now I tried this:

<?php
$transfercode = $block->transfercode();
$items = page('datenbank')->children()->findBy('documentIdentifierCode', '==', $transfercode);
foreach($items as $item): ?>
<?= $item()->title() ?>
<?= $item()->documentExtra()->kt() ?>
<?php endforeach ?>

this is error I’m getting on the page:

This is the structure of the “Datenbank” folder

As I wrote above, findBy() then returns a single page, not a collection to loop through, so

$transfercode = $block->transfercode();
$item = page('datenbank')->children()->findBy('documentIdentifierCode', '==', $transfercode);
if ($item) {
  dump($item);
}

I adjusted the code and still nothing happens :frowning:

Currently, trying to debug it, I reduced it to this:

<?php
$items = page('datenbank')->children();
foreach($items as $item): ?>
<?= $item()->title() ?>
<?php endforeach ?>

… and I put it to home.php template which serves home-page. Now I’m getting the error:

image

But I’m not getting why there is error, this is how the content folder looks like:

the parenthesis after $item are wrong

Damn :see_no_evil: now almost everything works as intended!

Is there any way to display the title of the file with corresponding document ID here instead of “Dateiabruf”?

You can add a label to your block, that would at least show the document id:

Blockdefinition:

name: Document ID
label: "{{transfercode}}"
fields:
  transfercode:
    label: Dokument ID
    type: text

For anything that goes beyond that, you would have to create a preview for your block.

1 Like

I have just a small additional detail. I figured that maybe more elegant solution would be to offer a multiselect of files, now I’m trying to understand query language. I read this guide: Query Language | Kirby CMS (getkirby.com)

Now I tried to

page('datenbank')->children()

transform into this query

transfercode:
  label: Dokument ID
  type: multiselect
  max: 1
  search: true
  options: query
  query: page('datenbank').children

but it won’t display children of page ‘datenbank’. What would be right way to query children of specific page?

This work though, but I’m worried if that would be a bit hard on sites performance as we expect 700+ files to be added.

query: site.index.filterBy('template', 'in', ['database.file'])
kirby.page('datenbank').children
1 Like

Thanks, as always fantastic support :slight_smile:

and sorry to bother you again, I implemented this solution to block blueprint

name: Document ID
label: "{{transfercode}}"

But the label isn’t showing what’s selected, instead it shows [object Object]

Why is it that way?

Because you have changed the field type from a simple text to a multiselect field which is returns as an object.

The label only works with simple values

When using a simple select field, it’s not possible to search it or dynamically show results like with multiselect field?

A select doesn’t have a search, but you can use the same query.

OK, that would be my feature suggestion for future versions :slight_smile: it would make the select field much more comfortable to use.

Thanks again :+1: