Returning custom fields as objects/arrays not as a string

I created a simple field blueprint:

label: Themes
type: structure
fields:
  title:
    label: Title
    type: text
    width: 2/3
  description:
    label: Description
    type: textarea
  image:
    label: Splash Image
    type: files
    min: 1
    multiple: false

And I used this in my homepage blueprint:

title: Home
icon: 🏠
options:
  changeStatus: false
sections:
  content:
    type: fields
    fields:
      Headline:
        type: text
      gap:
        width: 1/2
      themes:
        label: Themes
        extends: fields/frontpage-theme

This allows me to add multiple ‘themes’ (block with title, description, and image) to my front page in the panel. Works great.

But when I try to get that information on the frontend, using $page, it comes out as a single string called [themes] within the $page object: e.g:

Kirby\Cms\Page Object ( [content] => Kirby\Content\Content Object ( [title] => Home [headline] => XXXXXX [themes] => - title: Studio description: > XXXXXX. image: - file://xM62n8RFcvyGmH3Z - title: Workshops description: | XXXXXX. image: - file://PKcyGR4MPsABi3Aq [subheadline] => [uuid] => wljMYhm0m3e9QOwC ) [translations] => Kirby\Cms\Collection Object ( [0] => el [1] => en ) [children] => Kirby\Cms\Pages Object ( ) [files] => Kirby\Cms\Files Object ( [0] => home/studio.jpg [1] => home/workshop.jpg ) [id] => home [mediaUrl] => http://localhost/vessel/media/pages/home [mediaRoot] => /var/www/html/vessel/media/pages/home [num] => [parent] => [slug] => home [template] => Kirby\Template\Template Object ( [defaultType:protected] => html [name:protected] => home [type:protected] => html ) [uid] => home [uri] => home [url] => http://localhost/vessel/en [siblings] => Kirby\Cms\Pages Object ( [0] => photography [1] => notes [2] => about [3] => error [4] => home [5] => sandbox ) )

What I want to do is something like

foreach ($page->themes() as $theme) {
         $theme->title
         $theme ->description
         etc

What am I doing wrong?

Not reading the docs, I guess😉

Thank you for pointing me in the right direction.

I had spent quite a lot of time reading the docs. I don’t know how I was supposed to really find that information myself - even backtracking from your link to a solution that actually worked for me was complex. And while I am an amateur, I have plenty of web, PHP, and Kirby experience: I would gently suggest it is the docs at fault here, and you could (if you want) do a little better at either helping people navigate the complex documentation or being slightly more helpful when they come here for assistance.

For anyone else with this issue:

These fields I am attempting to create in the panel example above are called structures (this is not made clear in the starterkit example they are drawn from) and they are stored as yaml files.

When they are called by the frontend, they appear as hyphen-separated strings, as above

Applying toStructure() to these strings returns an empty array, I don’t know why. So the linked docs don’t actually help here.

Applying yaml() to the same data results in the desired outcome:

<?php foreach ($page->themes()->yaml() as $theme): ?>
      <div class="row block" id="<?=$theme['title']?>">
      <div class="column">
          <h3><?=$theme['title']?></h3>
          <p><?=$theme['description']?></p>
      </div>
    </div>
<?php endforeach ?>

EDIT: I’m not sure what was different, but I managed to get toStructure() to work, resulting in this scheme, which I think is preferred and allows you to keep using Kirby methods on the resulting data, e.g. to get an image file:


<?php foreach ($page->themes()->toStructure() as $theme): ?>

      <div class="row block" id="<?=$theme->title()?>" style="background-image: url(<?=$theme->splash()->toFile()->url()?>)">
      <div class="column">
          <h3><?=$theme->title()?></h3>
          <p><?=$theme->description()?></p>
      </div>
    </div>
<?php endforeach ?>

Sorry if it sounded offensive, wasn’t meant like that.

Here you create a field called structure. Each field has a section in the Reference: Fields | Kirby CMS

From this list of available fields, you get to the type you assigned to your field, i.e. structure.

Each field that is not very straightforward has a section how to use it in your templates.