@texnixe I’m trying to implement a similar function to the panel by following the function you wrote about here: Structure field update
But I can’t get i to work.
My function looks like this:
function addToStructure($pageURI, $field, $data = array()) {
kirby()->impersonate('kirby');
$fieldData = page($pageURI)->$field()->yaml();
$fieldData[] = $data;
$fieldData = yaml::encode($fieldData);
try {
page($pageURI)->update(array(
$field => $fieldData
)
);
return true;
} catch(Exception $e) {
return $e->getMessage();
}
}
And my hook like this:
'page.update:before' => function ($page, $values, $strings) {
if ($page == page('tag-manager')):
$globalTags = A::pluck($page->global_tags()->yaml(), 'tag');
$unique = array_unique($globalTags);
if (count($globalTags) !== count($unique)):
// There are duplicate entries
addToStructure($pageURI = 'tag-manager', $field = 'global_tags', $data = $unique);
endif;
endif;
}
And the blueprint with the structure field looks like this:
tags_section:
type: fields
fields:
global_tags:
label: Globala taggar
type: structure
sortBy: tag asc
fields:
tag:
label: Tag
type: text
If I enter two identical values to the structure field from the panel and hit save, the data is just saved, and the duplicates are still there. I’m using Kirby v3.2.3
What am I missing?