toStructure return nothing

Hi,

In my blog, I have a list of authors in my blog page with a structure field, like this :

  authors_tab:
    label: Auteurs
    icon: account

    fields:

      authors:

        label: Auteurs
        type: structure

        fields:
          name:
            label: Prénom
            type: text
            counter: false
          surname:
            label: Nom
            type: text
            counter: false
          mail:
            label: Mail
            type: text
            counter: false
          role:
            label: Rôle
            type: text
            counter: false
          description:
            label: Description
            type: text
            counter: false
          photo:
            label: Photo
            type: files

When I try to get the structure with:

<?php $authors = page('blog')->authors()->toStructure(); ?>

It return this instead of my structure field:

pre: Kirby\Cms\Structure Object
(
    [0] => 0
    [1] => 1
    [2] => 2
)

The field:

<?php $authors = page('blog')->authors(); ?>

Return this:

pre: Kirby\Cms\Field Object
(
    [authors] => - 
  name: Jean
  surname: Quette
  mail: email@email.com
  role: Butcher
  description: Lalalala
  photo:
    - file://qaWOtrVnoOot0QI9
- 
  name: Pierre
  surname: Ponce
  mail: email@email.com
  role: Butcher
  description: Lalalala
  photo:
    - file://Gcl5tMfAtDmxoV9S

I don’t understand how toStructure works :thinking:

Thanks!

See $field->toStructure() | Kirby CMS

I precise, my final goal is finding an author selected in a multiselect field in an article, who is returning an ID.

I was looking for a way to select the good author, that’s why I was searching to show the full structure.

Any idea ?

where does the id come from? Can’t see an id stored in your structure field.

But basically, you would find the authors from the authors field from a value in another field like this:

$selectedAuthors = page('blog')->authors()->toStructure()->filter(fn($item) => in_array($item->id(), $page->selectfieldName()->split(',')));

where does the id come from? Can’t see an id stored in your structure field.

The id(s) come from the multiselect of an article, in article.yml:

author:
  label: Auteur(s)
  type: multiselect
  options:
    type: query
    query: site.find("blog").authors.toStructure
    text: "{{ item.name }} {{ item.surname }}"

Your solution works well, thank you!
The only thing, this solution don’t keep the order of the items defined in the multiselect.

Hm, in that case, it might be better to loop through the values in the multiselect and fetch the corresponding item from the structure field one by one.

That’s what I tried to do before posting this topic, but no success :confused:

I tried to use $structure->find() and $structure->findByKey() too, but it return nothing.

Unfortunately, you didn’t post what you tried:

<?php

foreach ($page->multiSelectFieldName()->split(',') as $item) {

    if ($author =  page('blog')->authors()->toStructure()->findby('whatever here', $item) {
        echo $author->name();
    }
}

Still irritated by the ID, because structure field items don’t have an id.

OK I understand!

Thank you very much Sonja, works like a charm :pray: