Query blocks from another page

Hello there, I still have a small question. I created a block audio that contains a mp4 file on one page like Leçons. I now try to acsess those blocks in a block Mp4 from another page. I’m sure it’s easy (but not for me), I tried to work from the code below without success. Can you help me, thank you

  mp4:
    type: pages
    help: Audios de Espaces/ Mini-leçons depuis chez vous
    empty: "Aucune leçon pour l'instant"
    query: site.index.filterBy("blocks", "in", ["audio"])

Not really sure I understand what you would like to achieve.

You are using a pages field, so you want to query a set of pages, it seems. Pages that contain a specific block type?

I used pages knowing it might be wrong, what I try to do is query some blocks audio in a page Leçons from another page Essayez. thanks

Ok, then a pages field is not the right way to achieve that. For files you would need a files field. And then you need a custom method that you can use in your query.

What this custom method needs to do is find all audio blocks in a given page and of these blocks the field that contains the audio file information and convert this to a file object.

Ok I will look for that, thanks. So there is no way to query a complete block from another page?

If you want to query a block, that should be possible as well, via a select or multiselect field.

yes that is what i aim to do, but in the kirby reference page I don’t find out how to query blocks and how to filterby them, that is my question

Example:

  selectfield:
        type: select
        options: query
        query:
          fetch: kirby.page('notes').children.first.text.toBlocks.filterBy('type', 'text')
          text: "{{ block.text }}"
          value: "{{ block.id }}"

Thank you I will study this now!

I try to do my best but din’t succeed… part is I don’t get it yet, part I have got a structure in the block I want to query
so the blocks are in the child mini-lecons of espaces

fetch: kirby.page('espaces/mini-lecons').text.toBlocks.filterBy('type', 'text')

and this is the yaml of the audio block

name: Leçons
fields:
  title:
    type: text
  audio:
    label: Leçons
    type: structure
    fields:
      title:
        label: Titre
        type: writer
      text:
        label: Text
        type: writer
      source:
        type: files
        uploads: audio
        multiple: false
      pdf:
        label: PDF
        type: files
        uploads: files
        multiple: false

Can you help me query those blocks please?

First of all, what is your blocks field called? Is it called text or something else?

Then, you need to filter by type, i.e. audio.

But then it get’s more difficulat, because the audio field is a structure field and there we get to a point where the query language doesn’t help anymore, because you have a nested structure.

Therefore, you will have to create a custom method that extracts the audio blocks and then the information you need from the structure field.

Why do you need the nested structure in the audio block instead of creating a block per audio file?

Thank you for your patience. I did what you suggested, now each Leçon is in its block audio (audio.yml) and I try to query them like this

  fetch: kirby.page('espaces/mini-lecons').audio.toBlocks.filterBy('type', 'audio')

without succes, I get this error: Access to method/property filterBy on null in file: /Users/krsbee/Sites/feldenkrais/kirby/src/Toolkit/Query.php line: 242
:neutral_face:

Could you please post the blueprint for the mini-lecons page?

The error means the audio.toBlocks() does not return a collection, so my assumption is that your field is not called audio, so knowing everything is vital here.

So maybe your comment means that I didn’t get the page concept right yet… this is the adress of the mini-lecons page site.test/espaces/mini-lecons. Is it that in the query kirby.page('espaces/mini-lecons') instead of espaces/mini-lecons I should pasted the name of the template of the page which is contenu.yml ? That I try without sucess. Thank you

No, that’s not what I mean. Please copy here what you have in the blueprint for the mini-lecons page (if that’s contenu.yml, then that)

title: Articles
icon: page
options:
  changeTemplate:
    - contenucours
    - page
    - section
    - default

columns:
  main:
    width: 2/3
    sections:
    fields:
      extends: sections/intro

      principale:
        label: Colonne principale
        type: headline

      contenu:
        type: blocks
        pretty: true
        fieldsets:
          text:
            label: Texte
            type: group
            fieldsets:
              - texte
              - imageronde
              - quote
              - audio
              - mp4

Ok, your blocks field is called contenu, not audio, so that’s the field name you have to use in your query.

  fetch: kirby.page('espaces/mini-lecons').contenu.toBlocks.filterBy('type', 'audio')
1 Like

Once again thank you so much @texnixe and @pixelijn, you are so amazing. Have a great evening! :smiley: :pray: :watermelon:

hum… I still have two questions concerning this.
I didn’t find out how to call the nested block audio from mp4 I try foreach($block->mp4()->toBlocks() as $audio): without succes, can you point me to a solution, thank you
My second question is for the label for the mp4.yml, I try label: "{{ kirby.page('espaces/mini-lecons').contenu.toBlocks.filterBy('type', 'audio').title }}"
Many thanks