How to use UUID in structure entry?

Hey!

Thank you very much for your great cms! I really love the new unique id system in Kirby 3.8. Before this feature I used the the AutoID-plugin by bnomei. AutoID also works in structure fields. The new native feature also seems to be integrated in structures:

The Uuid classes provide an interface to connect identifiable models (page, file, site, user, blocks, structure entries) with a dedicated UUID string.

But I’m not sure how to use the UUID in the structure field.

districts:
	label: 
		en: Districts
		de: Bezirke
	type: structure
	fields:
		name:
			label: 
				en: Name
				de: Name
			type: text
	width: 1/4
	sortBy: name DESC
parks:
	label: Parks
	type: structure
	fields:
		name:
			label: 
				en: Name
				de: Name
			type: text
		district:
			label: 
				en: District
				de: Bezirk
			type: select
			options:
				type: query
				query: site.districts.toStructure
				text: "{{ structureItem.name }}"
				value: "{{ structureItem.uuid }}"

But I can’t find any uuid in the site.txt-file, where is the structure stored:

----

Districts:

- 
  name: Charlottenburg-Wilmersdorf
- 
  name: Friedrichshain-Kreuzberg
- 
  name: Lichtenberg
- 
  name: Marzahn-Hellersdorf
- 
  name: Mitte
- 
  name: Neukölln
- 
  name: Pankow
- 
  name: Reinickendorf
- 
  name: Spandau
- 
  name: Steglitz-Zehlendorf
- 
  name: Tempelhof-Schöneberg
- 
  name: Treptow-Köpenick

----

Because of the uuid in the query the editor fails and lists only one structure entry:

What am I doing wrong? And if the UUID is not supported in structures yet: What alternative do I have? The AutoID is already end-of-life. :neutral_face:

Thanks!

That’s exactly the case. UUIDs for the structure field will come in a later release.

Hm, good question. One way would be to add UUIDs using a hook in saving.

1 Like

Thanks for your quick reply! :grinning:

Hm, to code a plugin to add UUIDs via hooks sounds quite ambitious… Do you have an ETA for the UUIDs for the structure field?

No, I can’t tell. We would like to implement it as soon as possible, but UUIDs for structure fields are a bit tricky and need careful planning, that’s why they haven’t made it into 3.8.0 yet.

Looks like you can still use the AutoID plugin. Here’s a quote from Bruno:

  1. only the very latest autoid release is blocking k3.8 and that is ONLY if you use composer to install it. if you hardwire plugin v2.8.1 or download the zip you can keep using that even with k3.8 i decided create 2.8.2 with blocking k3.8 to emphasize that uuids are core now. (edited)
2 Likes

Is this still planned for the near future? I ran into an issue with the autoid plugin and php 8.2 yesterday so want to remove it from a site I’m maintaining. Thought I’d ask before I write my own plugin to deal with structure UUIDs.

I don’t think so, as far as I know it is not planned for the Kirby 4 release.

I know it’s been a while since the last reply here. Are the docs incorrect? It notes structure entries as one of the things that utilizes UUIDs

Yes, sorry, that information was either wishful thinking or added a bit prematurely.

1 Like

Update for Kirby 4.5: Adding UUIDs with a Hook

Here’s an update for Kirby 4.5. Simply add a hidden field to the blueprint:

uuid:
  type: hidden

Then, create a hook for site.update:after, page.update:after, or similar events. Below is an example of a hook. You only need to adjust it to fit your specific needs:

use Kirby\Data\Data;
use Kirby\Uuid\Uuid;

return [
    'site.update:after' => function (Kirby\Cms\Site $newSite, Kirby\Cms\Site $oldSite) {
        $employees = $newSite->employees()->yaml();

        foreach ($employees as $item) {
            // Check if a valid UUID already exists
            if (empty($item['uuid'])) {
                $item['uuid'] = Uuid::generate();
            }
        }

        $newSite->save([
            'employees' => Data::encode($employees, "yaml"),
        ]);
    } 
];

Important Note

It’s crucial to include the hidden field in the blueprint. Without it, the hook will continuously generate and save a new UUID every time an update occurs.

Please note that this forum is in English, we don’t really want people to start adding translations for all sorts of languages following your example. Thanks for your understanding :slightly_smiling_face:.

1 Like