Blueprint: Title of a tag that is assigned by a page query

Is there an easy way to display the titles of the selected tags on the parent page? kat: true works but then I get something like works/urban. But I would like to show the titles. I’ve tried a few things with the query, but I’m not getting anywhere. Here is one of the unsuccessful versions.

# parent

pages:
  type: pages
  layout: table
  columns:
    kat: 
      query: page.kat.toPages
      value: "{{ item.title }}"
# child

kat:
  type: tags
  options: query
  query: site.page("works").children

The logic is a bit more than I would put in a blueprint query, so the first thing I would recommend is creating a page model for the child page (the one that has the kat field).

In this, I created a method that should return the selected pages’ titles as string separated by commas. This format works best to then be displayed in the column as tag bubbles:

<?php

use Kirby\Cms\Page;

class YourTemplateNamePage extends Page
{
	public function katTitles(): string
	{
		$titles = $this->content()->get('kat')->toPages(',')->values(
			fn (Page $page) => $page->title()
		);

		return implode(', ', $titles);
	}
}

What’s happening here:

We get the kat content field and apply ->toPages() to turn the selected pages into actual page objects. We need to pass ',' as parameter so the toPages method knows the separator between the different pages. By default it expects a yaml array (e.g. from the pages field), but here you use a tags field which stores its values as comma-separated string.

The result is a pages collection of page object. We then apply ->values() to it which allows us to get for each page object the page title. The result of this call then is an array with the page titles from the selected pages.

And lastly we concat those titles again as a comma-separated string.

All you need then in your parent blueprint is calling this new method of your child pages in the value query:

columns:
  kat:
    value: "{{ page.katTitles }}"

This would be a solution that comes to mind using the tags field. Of course, you could also think about using a pages field instead which would get you this result much more out-of-the box.

1 Like

Wow, that’s super cool. Thank you very much for your help.