Skip to content

Instantly share code, notes, and snippets.

@lukaskleinschmidt
Last active April 14, 2024 14:26
Show Gist options
  • Save lukaskleinschmidt/1c0b94ffab51d650b7c7605a4d25c213 to your computer and use it in GitHub Desktop.
Save lukaskleinschmidt/1c0b94ffab51d650b7c7605a4d25c213 to your computer and use it in GitHub Desktop.
Kirby 3 Synced Structure
<?php
use Kirby\Form\Field;
function uuid_structure(array $data): array
{
$result = [];
foreach ($data as $value) {
$uuid = $value['uuid'] ?? uuid();
if (isset($result[$uuid])) {
$uuid = $value['uuid'] = uuid();
}
$result[$uuid] = $value;
}
return $result;
}
function sync_structure(Field $field, array $data): array
{
$kirby = kirby();
$currentLocale = $kirby->languageCode();
$fields = $field->fields;
$model = $field->model;
$name = $field->name;
$update = [];
$data = uuid_structure($data);
foreach ($kirby->languages()->codes() as $locale) {
if ($locale === $currentLocale) {
$update[$locale] = $data;
continue;
}
if (! $model->translation($locale)->exists()) {
continue;
}
if ($model->content($locale)->{$name}()->isEmpty()) {
continue;
}
$values = $model->content($locale)->{$name}()->yaml();
$values = uuid_structure($values);
foreach (array_keys($data) as $uuid) {
$update[$locale][$uuid] = $values[$uuid] ?? $data[$uuid];
}
}
foreach ($update as $locale => $values) {
$update[$locale] = sync_structure_fields($fields, $values, $data);
if ($locale !== $currentLocale) {
$model->save([
$name => array_values($update[$locale]),
], $locale);
}
}
return array_values($update[$currentLocale]);
}
function sync_structure_fields(array $fields, array $values, array $data): array
{
foreach ($fields as $name => $field) {
$sync = $field['sync'] ?? false;
if ($sync === false) {
continue;
}
foreach (array_keys($values) as $uuid) {
if (isset($data[$uuid][$name])) {
$values[$uuid][$name] ??= $data[$uuid][$name];
}
}
}
return $values;
}
panel.plugin('lukaskleinschmidt/synced-structure', {
fields: {
'synced-structure': {
extends: 'k-structure-field'
}
}
});
<?php
use Kirby\Cms\App;
use Kirby\Form\Form;
@include_once __DIR__ . '/helpers.php';
App::plugin('lukaskleinschmidt/synced-structure', [
'fields' => [
'synced-structure' => [
'extends' => 'structure',
'methods' => [
'form' => function (array $values = []) {
$uuid = uuid();
$fields = $this->attrs['fields'] + [
'uuid' => [
'type' => 'hidden',
'default' => $uuid,
],
];
foreach ($fields as &$field) {
if (isset($field['translate'])) {
$field['sync'] = ! $field['translate'];
unset($field['translate']);
}
}
$values['uuid'] ??= $uuid;
return new Form([
'fields' => $fields,
'values' => $values,
'model' => $this->model,
]);
},
],
'save' => function ($value) {
$data = [];
foreach ($value as $row) {
$data[] = $this->form($row)->content();
}
if (kirby()->multilang() === true) {
return sync_structure($this, $data);
}
return $data;
},
],
],
]);

MIT License

Copyright (c) 2023 Lukas Kleinschmidt

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment