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 …
Thanks!
texnixe
December 27, 2022, 2:57pm
2
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 ?
texnixe
December 27, 2022, 4:26pm
4
theamnesic:
who is returning an ID.
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.
texnixe
December 27, 2022, 5:03pm
6
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
I tried to use $structure->find() and $structure->findByKey() too, but it return nothing.
texnixe
December 27, 2022, 5:47pm
8
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