Fill page title by combining two other page fields


#1

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.

Here the final syntax on the output page

<?php foreach($authors as $author): ?>
  <a href="<?php echo $author->url() ?>">
    <?php echo $author->title()->html() ?>
  </a>
<?php endforeach ?>

and the other would be

<?php foreach($map_author as $key => $name): ?>
  <a href="<?php echo url($authors . '/' . (str::slug($name[1] . ' ' . $name[0]))) ?>">
    <?php echo html($name[1] . ' ' . $name[0]) ?>
  </a>
<?php endforeach ?>

What would be a possible way? I am ofc trying to minimise the amount of double content to add when making a new entry on the panel.

Thanks!


#2
title: {name} {surname}

This would be actually a pretty cool feature! Not possible at the moment though :frowning:

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.


#3

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?


#4

@pedroborges
thank you! But as @texnixe points out, your suggestion works the other way around of what I had in mind.

My only preoccupation would be to not have the client typing twice the same text when they create a new page.

And thinking once more about the array map technique, whenever an author would have a two word name or surname, for eg, array map would probably fail.

I guess it’s not possible :smiley:


#5

I had the same problem a while ago and decided that the user just has to add the full name as title and name + surname in separate fields.


#6

Yes exactly!

No big deal, but while thinking about the user workflow I thought it would be a sweet thing to have this kind of {query} option.


#7

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.


#8

I’m pretty confident there will be updates also for these kind of options, sometime soon.

:slight_smile:


#9

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).


#10

Problem with using users is that you have to add an email and they clutter the user list in the panel.

Edit: I think there is a feature request somewhere to get rid of the modal altogether.


#11

Thanks so much for this code

I can confirm, this is working perfectly!