Hello again
yesterday I learned from @Texnixe how to get default content for a structure field in a multilingual setup by defining the translations inside a blueprint. (Structure fields default values in multilingual setup)
Now I want to go a step further and integrate a kind of taxonomy tab on my site blueprint to have a more flexible way of defining default content.
My blueprint looks like this:
- width: 2/4
sections:
projectsSpecifications:
type: fields
fields:
specifications:
label:
de: Spezifikationen
en: Specifications
type: structure
translate: false
fields:
de:
label:
de: Deutsch
en: German
type: text
width: 1/2
en:
label:
de: Englisch
en: English
type: text
width: 1/2
this gives me a language specific structure:
-
de: Spezifikation 1 (DE)
en: Specification 1 (EN)
-
de: Spezifikation 2 (DE)
en: Specification 2 (EN)
# [...]
For each language project.txt I need something like this:
- headline: Spezifikation 1 (DE)
- headline: Spezifikation 2 (DE)
# [...]
The problem: I don’t know how to convert one structure field logic to another one. I was already trying to use:
foreach (kirby()->languages() as $language) {
$structure = $this->site()->specifications()->pluck($language->code());
//...
}
to just get the language specific headlines. Or is it better to Filter? I don’t know! And what steps are missing to get a valid structure to
$page->update([
'specifications' => yaml::encode($array)
], $language->code());
Thanks for your great and fast support, happy hearing from you!
Do you want to convert your content from structure B to structure B? But then your field structure won’t work anymore.
I want to have a taxonomy tab on my site where it’s possible to define some content for the languages. The languages are limited to German and English.
this inside my site.de.txt (the translation is set to false in my blueprint, so that the user can define the translations next to each other.)
-
de: Spezifikation 1 (DE)
en: Specification 1 (EN)
-
de: Spezifikation 2 (DE)
en: Specification 2 (EN)
# [...]
This content should always be used as a default content for a structure field on a projects page on page creation. (simply predefined headlines for each language.)
So the following content has to be generated by a hook.
this inside my project.de.txt:
- headline: Spezifikation 1 (DE)
- headline: Spezifikation 2 (DE)
# [...]
and inside my project.en.txt:
- headline: Specification 1 (EN)
- headline: Specification 2 (EN)
# [...]
(hope it’s more clear now?)
Or is there a better way to solve the idea of a taxonomy tab? Did it as well for the tags by having a structure field on my site blueprint and query it on my project page.
tags:
label: Tags
type: multiselect
min: 1
translate: false
accept: options
options: query
query:
fetch: site.tags.toStructure
text: "{{ structureItem.de }}"
value: "{{ structureItem.en }}"
As far as I understood it right on other threads this would be the optimal way of defining global tags. Would be nice to have a similar solution for my structure translations.
Right now I’m getting closer, but still missing some knowledge:
'hooks' => [
'page.create:after' => function ($page) {
if ($page->intendedTemplate()->name() === 'project') {
foreach (kirby()->languages() as $language) {
$structure = $this->site()->specifications()->toStructure()->pluck($language->code());
$string = '- headline: ' . implode(",- headline: ", $structure);
$array = explode(',', $string);
$page->update([
'specifications' => yaml::encode($array)
], $language->code());
}
}
}
]
This gives me following content inside my project.de.txt:
Specifications:
-
headline: null
content: null
0:
headline: Spezifikation 1 (DE)
-
headline: null
content: null
0:
headline: Spezifikation 2 (DE)
Maybe someone can help me how to get it right from here?
Ah, got it!
'hooks' => [
'page.create:after' => function ($page) {
if ($page->intendedTemplate()->name() === 'project') {
foreach (kirby()->languages() as $language) {
$structure = $this->site()->specifications()->toStructure()->pluck($language->code());
$string = '- headline: ' . implode("\n- headline: ", $structure);
$array = yaml::decode($string);
$page->update([
'specifications' => yaml::encode($array)
], $language->code());
}
}
}
]