Get title form parent page tags

Hey.

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

and if I check

$item->subcategory()

I get “gaertner”.

But i need the title of the tag not the slug.

Maybe someone has me a hint :slight_smile:

I’m afraid I don’t understand. Are the categories pages? Or where is the title supposed to come from?

NP :slight_smile: I will add some screenshots

On the PartnerOverview site I have tags.

And I use them on the Child pages. (Partner)

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);
   }
?>
print_r($field['options']);
// Undefined array key "options"

I think the main problem is, that the tags are stored in the page and not in the field.

That give me all tags that a set in a row coma seperated.

$subcat = page('partner')->subcategories();
print_r($subcat);

And I was thinking that I can get over that the value. And over a config map I can’t do it cause customer is free to add more tags.

Maybe you have a hint for me @texnixe ?

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;
}
1 Like

@texnixe here is my yml and now I see - I use there .slug - I guess there is no way to go back to value without going trough each child

subcategory:
            label: Unterkategorie
            type: select
            options:
              type: query
              query: page.parent.subcategories.split
              value: "{{ arrayItem.value.slug }}"

I will try your code. Thanks :slight_smile:

Thank’ s your code works perfct.