Is it possible to import all structure field data into another structure field via a single query?

Hi there,

I am sort of struggling with queries inside the structurefield right now.
Essentially, what I want to do is have an employee list with all their data be managed in one structure field. Same thing also applies to a list of locations with certain data pertaining to each location.

Using Tim Öttings page builder I have tried to create my own block which should be able to call all employees / locations and store them in its own structure field. The reason for that being that I want this list to allow rearrangement of its content.

Now, when I try to render everything in my snippet I start running into issues.
Apparently the way I am querying the structure item is wrong.
When using dump() I get the following:

Kirby\Cms\Field Object
(
    [employeelist] => Array
        (
            [0] => Array
                (
                    [employee] => 0
                )

        )

)

This is how I have my structure fields set up in the site.yml:

employees:
        label: Mitarbeiter
        icon: users
        fields:	
            employees:
                type: structure
                style: table
                fields:
                    name: 
                        label: Name
                        type: text
                        width: 1/3
                    lastname: 
                        label: Nachname
                        type: text
                        width: 1/3
                    profilepic: 
                        label: bild
                        type: files
                        query: site.images
                        width: 1/3
                        max: 1
                        layout: cards
                        size: small
                    position:
                        label: Position
                        type: text
                    telnumber: 
                        label: Telefonnummer
                        type: tel
                        width: 1/3
                    mobilenumber: 
                        label: Handynummer
                        type: tel
                        width: 1/3
                    email: 
                        label: E-Mail
                        type: email
                        width: 1/3
                    experience: 
                        label: Erfahrung
                        type: list
                        width: 1/2
                    qualifications: 
                        label: Qualifikationen
                        type: list
                        width: 1/2
    locations:
        label: Standorte
        icon: home
        fields:	
            locations:
                type: structure
                style: table
                fields:
                    locationphoto: 
                        label: Bild
                        type: files
                        query: site.images
                        max: 1
                        layout: cards
                        size: small
                        width: 1/2
                    locationtype:
                        label: Standort-Art
                        type: radio
                        width: 1/2
                        options:
                            showroom: Showroom
                            headquarters: Hauptsitz
                            other: Anderes
                    locationother: 
                        label: Standort-Art
                        type: text
                        when: 
                            locationtype: other
                    locationstreet: 
                        label: Straße
                        type: text
                        width: 1/3
                    locationextra: 
                        label: Zusatzinformation
                        type: text
                        width: 1/3
                    locationpostal: 
                        label: PLZ
                        type: text
                        width: 1/3
                    locationcity: 
                        label: Ort
                        type: text
                        width: 1/3
                    locationtel: 
                        label: Telefonnummer
                        type: tel
                        width: 1/3
                    locationemail: 
                        label: E-Mail
                        type: email
                        width: 1/3

This is my pagebuilderList.yml:

label: Liste
fields:
    headline:
        label: Titel
        type: text
        width: 2/3
    draft:
        label: Entwurf
        type: toggle
        default: false
        width: 1/3
    subheadline:
        label: Untertitel
        type: text
    colormode:
        label: Farbmodus
        type: radio
        options:
            white: Weiße Kacheln
            grey: Graue Kacheln
            blue: Blaue Kacheln
        default: white
        columns: 3
    listtype:
        label: Listenmodus
        type: radio
        options:
            employees: Mitarbeiter
            locations: Standorte
            services: Leistungsbereiche
            other: Allgemein
        columns: 4
        default: other
    employeelist:
        label: Auflistung der Mitarbeiter
        type: structure
        when:
            listtype: employees
        fields:
            employee:
                label: Mitarbeiter
                type: select
                options: query
                query: 
                    fetch: site.employees.toStructure
                    text: "{{ structureItem.name }} {{ structureItem.lastname }}"
                    value: "{{ structureItem }}"
    locationlist:
        label: Auflistung der Standorte
        type: structure
        when:
            listtype: locations
        fields:
            locations:
                label: Standort
                type: select
                options: query
                query: 
                    fetch: site.locations.toStructure
                    text: "{{ structureItem.locationstreet }} {{ structureItem.locationcity }}"
                    value: "{{ structureItem }}"
    servicelist:
        label: Auflistung der Leistungsbereiche
        type: structure
        when:
            listtype: services
        fields:
            services:
                label: Leistung
                type: select
                options: query
                query:
                    fetch: site.children.template('service')
    otherlist:
        label: Allgemeine Liste
        type: structure
        when:
            listtype: other
        fields:
            otherlistimage:
                label: Bild
                type: files
                query: page.images
                max: 1
            otherlistimageclass:
                label: Bildart
                type: radio
                options:
                    otherlisticon: Icon
                    otherlistimg: Normales Bild
                default: otherlistimg
            otherlisthead:
                label: Headline
                type: text
            otherlisttext:
                label: Text
                type: textarea
            otherlistbutton:
                label: Button
                type: toggle
            otherlistbuttontxt:
                label: Button-Text
                type: text
                width: 1/2
                when:
                    otherlistbutton: true
            otherlistbuttonlink:
                label: Button-link
                type: link
                width: 1/2
                when:
                    otherlistbutton: true
        
snippet: pagebuilderList

This is a snippet of my pagebuilderList.php snippet:

<?php $colorMode = $data->colormode()->value() ?>
<section class="viewPagebuildernewsList<?= snippet("pagebuilder-draft", array("data" => $data)) ?> 
	<?php 
		switch ($colorMode): 
			case "white":
				echo " c2sWhite";
				break;
			case "grey":
				echo " c2sGrey";
				break;
			case "blue":
				echo " c2sBlue";
				break;
		endswitch;
	?>">
	<div><?= dump($data->employeelist()) ?></div>
	
	<?php if ($data->headline() != ""): ?>
	   <h2><?= $data->headline() ?></h2>
	<?php endif; ?>
	   
	<?php
		$uid_array = explode("_", $data->_uid()->value());
		array_shift($uid_array);
		$uid = implode("", $uid_array);
	?>
	   
	   
	<ul class="list">
	<?php 
		switch($data->listtype()):
	   		case "employees":
				$list = $data->employeelist()->toStructure();
	?>
					
		<?php
			foreach ($list as $item):
		
		?>
		<li class="">
			<figure class="lazy" <?php if ($img = $item->profilepic()->toFile()): ?> data-src="<?= $img->resize(500, 500)->url() ?>"<?php endif; ?> ></figure>
			<div>
				<h4><?= $item->name() ?> <?= $item->lastname() ?></h4>
				<h5><?= $item->position() ?></h5>
				<div class="mobile"><?= $item->mobilenumber() ?></div>
				<div class="telnumber"><?= $item->telnumber() ?></div>
				<div class="employeemail"><?= $item->email() ?></div>
			</div>
		</li>
		<?php
			endforeach;
		?>

What is it that I am doing wrong here?
Is it even possible to query all the data of a structure field to another with just one query?

Two problems:

As value you try to store the item, which doesn’t make sense, store a field name here that is unique, ideally an id.

Then in your PHP part, you fetch the item by this unique identifier and this will then give you access to the information stored in this item in the the site field.

Thanks for pointing that out.

I still have some issues trying to fetch everything now.
To get a unique id, I’ve installed @helllicht 's autoid plugin.

Now the way I thought I could fetch everything by that given ID was taking said ID and using $site->findBy(). But I seem to be doing something wrong still.

$id = $data->employeelist()->toStructure(); // ->value() returns nothing 
$employee = $site->findBy('autoid', $id);
dump($id);

scratching the ->toStructure returns an object of all employee IDs looking like this:

Kirby\Cms\Field Object
(
    [employeelist] => Array
        (
            [0] => Array
                (
                    [employee] => h4m2y97r
                )

            [1] => Array
                (
                    [employee] => lbim2nwa
                )

            [2] => Array
                (
                    [employee] => slybptqc
                )

        )

)

with what my dump() returns the issue is presumably in how I define $id.
How should I rearrange /rewrite everything so that I can find all listed employees and then list them one after another?

I managed to do it mixing two foreach loops.

now it looks like this:

	<?php 
		switch($data->listtype()):
	   		case "employees":
	   
	   			$ids = $data->employeelist()->toStructure();
		   
				$list = $site->employees()->toStructure();
				
				foreach($ids as $id):
					foreach ($list as $item):
						if($item->autoid()->value() === $id->employee()->value()):
		?>

Though having a foreach inside a foreach seems awfully unoptimized…
Is there a better or more efficient way to do this?