Hi,
is it possible to fill the title field by using two other fields present in the same blueprint page?
I have an author page, with title name surname. I thought of adding two more fields called name and surname, so I can easily order a foreach loop of all the authors by surname.
I was wondering if it was possible to automatically fill the title field by adding on the blueprint a construct such as:
title: {name} {surname}
wherein upon making a new author page, the two required fields would be name and surname, and the highly required title field would be auto-filled once the other two fields are being completed.
The other way would be to adjust this reply that uses an array map with the title field instead of the tag field.
The only problem I encountered was that I need to do two foreach loops, as in one I am building the object containing the url that links to that page, and the other to loop through the array map version.
This would be actually a pretty cool feature! Not possible at the moment though
You can use a hook to change the title every time the page is updated:
$kirby->set('hook', 'panel.page.update', function ($page) {
// Assuming you blueprint is called 'author', change if needed
if ($page->intendedTemplate() === 'author') {
$title = $page->name() . ' ' . $page->surname();
$uri = str::slug($title);
// Avoid updating when a sibling page with same title already exists
if (! $page->parent()->find($uri)) {
$page->update(compact('title'));
$page->move($uri);
}
}
});
The hook can be placed on a site/plugins/hooks.php file.
The question is if this makes sense, because you will probably create the page with name and surname in the first place to avoid any problems with duplicate pages, I assume?
Yes, I agree absolutely, but as long as we have the modal that forces us to add a title (you can’t even set a default value), there’s no way that makes sense.
Users is definitely better, it’s what I prefer to use.
In the context I used this hook I needed the title to be a formatted date. On creating a page the user could add any title and from the first update the title would always reflect the formatted value of a date field. On the blueprint I removed the title field so it didn’t show on the panel at all.
It would be great to have a blueprint for the page creation modal:
title: Page
modal:
title: {name} {surname}
fields:
name:
label: Name
type: text
surname:
label: Surname
type: text
fields:
name:
label: Name
type: text
surname:
label: Surname
type: text
# ...
I’m sure there are better name for these keys… But you get the idea! The title would no longer be required and you could specify from which fields the Panel could make a title from (e.g. for subpages listing).