Call nested fields from page builder

#1

I think there’s sth wrong how I call the historyimagepair

  <?php foreach($data->img()->toFiles() as $data): ?>
    <figure>
      <img srcset="<?= $image->srcset() ?>" />
      <figcaption><?= $data->caption() ?></figcaption>
    </figure>
  <?php endforeach ?>

And this is the blueprint

title: Kirby Builder
preset: page
pages: false
fields:
  headline:
    extends: fields/headline
  subhead:
    extends: fields/subhead
  mybuilder:
    label: Content of the section
    type: builder
    columns: 1 
    max: 12 
    help: Title + Text are mandatory, images optional
    fieldsets:
      bodytext:
        label: Text
        fields:
          text:
            extends: fields/text
      historyimagepair:
        label: History Imagepair
        fields:
          imagelist: # The Builder Field can even be nested!
            type: builder
            label: Image List
            columns: 2
            max: 2
            fieldsets:
              img:
                label: img
                help: Ein Querformat oder zwei Hochformat
                fields:
                  images:
                    label: Images
                    type: files
                    empty: No images selected yet!
                    min: 1
                    max: 1
                    layout: cards
                  caption:
                    label: Caption
                    type: text

naming is not quite smart at this point

Output is an empty div

0 Likes

#2

Please post your complete template code, that snippet alone doesn’t make much sense because we don’t see how $data is defined.

0 Likes

#3

Oh, I am sorry!

This is how the pagebuilder is called…

  <?php foreach($data->mybuilder()->toBuilderBlocks() as $block):
  snippet('blocks/' . $block->_key(), array('data' => $block)); 
  endforeach; ?>

…and it does call the historyimagepair.php snippet shown above, but the snippet itself shows nothing.

I hope it’s what you were thinking of!

0 Likes

#4

That’s because you are trying to call a single field in the nested builder when you have to get the snippets for the nested builder snippets/fieldsets in your historyimagethinby snippet first.

mybuilder is your top builder field.

0 Likes

#5

Does that mean I first have to call the mybuilder blocks in the historyimagepair.php and than create another sets of snippets for images and caption?

(btw if there’s better way to use captions, I’d also like to know it)

0 Likes

#6

Well, you could also add the captions in the file meta data, that depends a bit on your workflow.

0 Likes

#7

I tried this and it didn’t work. I am absolutely clueless, especially since I found no example for nested page builder fields anywhere.

You mean like in the file file? But how would I translate it than? Does not seem to be the most elegant solution…

0 Likes

#8

You can translate file meta data just like you can translate page content, that’s not the issue. But whether or not that is the most elegant solution or not, that depends on what you want to achieve.

I haven’t used a nested builder field yet, so I can’t really give you advice from the top of my head without testing it first.

0 Likes

#9

Does “metadata” in this case mean the EXIF/IPTC data from the jpg or is my head working in the wrong direction? Could you give me a hint for the meta data thing and how to call it? I am kind of clueless, even where to start searching.

I thought this nested field was rather a standard operation… so now I absolutely understand you!

0 Likes

#10
0 Likes

#11

Thanks again @texnixe!
It’s not the most intuitive way (regarding the panel) but it’s sufficient for my needs right now.

0 Likes

#12

This is what your historyimagepair.php snippet would probably have to look like (for your original nested builder)

<div>
  <?php foreach($block->imagelist()->toBuilderBlocks() as $subBlock): ?>
    <!-- this is the snippet for the _img block -->
    <?php if($file = $subBlock->images()->toFile()): ?>
      <figure>
      <img src="<?= $file->url() ?>" alt="">
      <figcaption><?= $subBlock->caption()->kt() ?></figcaption>
      </figure>
    <?php endif ?>
    <!-- end of images block snippet -->
  <?php endforeach ?>
</div>

Note that I use $block instead of $data (you would have to change that to fit your variable)

This will work without additional snippets, because inside the nested fieldset, there is only a single fieldset. If you were to use different fieldsets, you would have to call the corresponding snippets again.

0 Likes

#13

Thank you!

0 Likes