On the parent page, I have a tag field, and in the child pages, I can assign tags. Now, I want to pass the title, but when I loop through the child pages, I only see the slugs of the tags in the subcategory field. I tried using the split() method, but now I need to somehow check which one of them is correct.
page('partner')->subcategories()->split();
// gives me the array
$item->subcategory() // has the slug of the tag i need
For example page('partner')->subcategories()->split();
gives me an Array [0] - Gärnter [1] - Architekt
When I output in the Partner page all child i only have in the subcategory field of the Child the slug out of the Parent tags - but I want to output “Gärtner” not “gaertner”.
foreach($allpartner as $partner) {
$partner->subcategory();
}
Ok, then you need to fetch the display value from the options in your blueprint, or define the key/value place somewhere else, e.g. in your config, see also
Oky I tried that - but I does not output the label i goes over and output directly the html()
<?php
// Parent where all available tags are stored
$field = page('partner')->blueprint()->field('subcategories');
// Where tag in child is set
$categories = $item->subcategory()->split(',');
foreach ($categories as $category) {
echo $field['options'][$category] ?? html($category);
}
?>
Not sure why you sluggify the subcategories in the parent page.
But you can create a category map on the fly like this:
// fetch the subcategories from the parent page into an array
$parentCats = $page->parent()->subcategories()->split(',');
// sluggify each item in the array
$parentCatSlugs = array_map(fn($item) => Str::slug($item), $parentCats);
// combine the two array, using the sluggified version as key, the orginal ones as values
// will result in
// array:2 [▼
// "gartner" => "Gärtner"
// "architekt" => "Architekt"
//]
$categoryMap = array_combine($parentCatSlugs, $parentCats);
$categories = $page->subcategory()->split(',');
foreach ($categories as $category) {
// fetch by key
echo $categoryMap[$category] ?? $category;
}