->toPages() in multilevel structure

How can I use the ->toPages() method on a deeply nested mutlilevel structure field like this:

  header:
    label: Header
    type: structure
    max: 1
    fields:
      menu:
        label: Navigation
        type: structure
        max: 1
        fields:
          links:
            type: pages
            max: 6

The problem I am having the the following: When turning the header strucure to a array with this:
<php foreach($site->header()->yaml() as $header):?>

Then I won’t be able to use toPages() like this:
<?php foreach($header['menu']->toPages() as $menu):?>
Since the Textfile is now transformed into an array.

Is there a way to reverse this and call ->toPages() on a multilevel structure field?

I haven’t used nested structure yes, but I’d recommend using toStructure() instead of yams().

The same problen will persist. Since when you call toStructure in the end I will have an array of my “menu” field. And since ->toPages() required the “default” yaml text format just like it is saved in the /content/site.txt and not a array/yaml or object to be given I cannot use ->toPages or ->toPage on this.

I would need some way so that in the end I still get:
related:
- fur-endkunden
- fur-finanzdienstleister

Because only then I can call ->toPages.

You have to loop through the structure like this:

<?php

$str = $site->header()->toStructure();
foreach($str as $item) {
  foreach($item->menu()->toStructure() as $menu) {
    dump($menu->links()->toPages());
  };
}
?>
1 Like

@Ebbel Did you know we have a “Question :thinking: :thinking: ” category for questions? Would be great if you could choose the question category that corresponds with your Kirby versions. Helps us to keep track and answer your question faster. Thanks.