Showing specific Builder snippets by type

I’m struggling to display content from the wonderful Builder plugin outside of the foreach loop used for calling the blocks.

I have a field called mix that has a subfield called title. I want to display the title in nav.php.

My article.yml blueprint defines all the fields:

fields:
 content:
  builder:
   label: Page Builder
   type: builder
   fieldsets:
    mix:
     label: Mix
     fields:
      info:
       type: text
       label: Title
      upload:
       type: files
       multiple: false
       label: Upload mix
       layout: list
       query: page.audio
    textarea:
     label: Paragraph
     fields:
      textarea:
       type: textarea
       size: medium
       label: Paragraph

In article.php, the foreach loop outputs all the content I build:

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

And it works just fine. However, when I try to get the contents of the subfield info to display in nav.php, it doesn’t work:

<div class="ticker-wrap">
 <?php foreach($page->builder()->toBuilderBlocks()->data() as $block): ?>
  <?php foreach($block->mix() as $ticker): ?>
   <?= $ticker->info() ?>
  <?php endforeach ?>
 <?php endforeach ?>
</div>

I get the debug message Call to a member function info() on null.

I can’t wrap my head around what happens between toBuilderBlocks and data and would greatly appreciate some assistance.

Thanks in advance!

Not quite sure what you are trying to achieve here, but if you call data() after toBuilderBlocks(), you end up with an array and hence can’t use -> syntax afterwards. Do you only want to get builder items of type mix?

If so, you can filter the blocks by _key

<div class="ticker-wrap">
 <?php foreach($page->builder()->toBuilderBlocks()->filterBy('_key', 'mix') as $block): ?>
   <?= $block->info() ?>
  <?php endforeach ?>
</div>

This way, you only loop through fieldsets of type “mix”.

Data in this bit is the variable you will use in your snippet, which is assigned the $block item, maybe this is what got you confused?

Data in this bit is the variable you will use in your snippet, which is assigned the $block item, maybe this is what got you confused?

Ah, that’s exactly it – I completely misunderstood the link between the two. As always, thank you so much @texnixe!