Kirby3 blueprint text field dynamic default value from page title

Hi all,

Back with another question, ha… I can get the dynamic (newly created page title) passed into the ‘placeholder’ and ‘help’ props of my blueprint, but the ‘default’ prop isn’t working:

titleLine1:
  type: text
  label: Title Format
  default: "{{ page.title }}"
  placeholder: "{{ page.title }}"
  help: The {{ page.title }} shows up here too!
  counter: false
  width: 1/2

Default value not showing “Abc 123” (page title):

But placeholder works when I clear out the field:


I’m looking into the page hook create:after plugin stuff, but that isn’t working either.

As always, thanks for your help :zap:

I think in most cases, you can only use a string as default value. I’m not even sure if the title even exists at the time Kirby determines the default values for a new page.

This is what’s so confusing about Kirby… I mean, if you can interpolate the placeholder and help props, why can’t you just snag the value for the default prop value? In my screenshot I can literally see the title being rendered as placeholder and in the help field.

I’ve been burning so many hours debugging little/big things like this, hah, feel like Kirby 2 was 100x easier to use. But I’m torn, because I’m using Jakub’s super nice Kirby Vue Starterkit, so I’m trying to like that K3 is built with Vue, but I’m so confused with issues like this… ha

So, can I use a page.create:after hook to drop that in to the default value? I mean I tried this but it didn’t work, but there should be some way to get this if the placeholder and help props can render it.

1 Like

Yes, you can use either a hook or a page model.

However, what’s the purpose of having a second field with the value of the title?

Haha, excellent question :sweat_smile: This was a “client request” to format the title on 2 lines in certain areas…

For instance, a page title may be
Project Name - A Fun Title

But on a certain area of the site, the title will be formatted to their liking on two lines:

Project Name
A Fun Title

They may want a hyphen in the original title for SEO / browser title, but in the format area on two lines they want to delete the hyphen and drop on two lines etc.

So, my goal was to auto-populate a small textarea field so the first line was the full title, then they could hit a hard return so then I could use the $data->title()->kt() and break this out on two lines.


So, I’m really sorry to trouble you, but can you provide an example of the page.create:after hook (or another hook?) in how I could grab the page title to use in my blueprint? I tried a few ways but no luck.

Thanks a million @texnixe!

Like in the date hooks in the other thread, you would have to update the page. Or you adapt the model I provided in the same thread. What’s best depends…

Hook: Can more easily be applied to multiple page types if necessary.
Page model (create method): Modifications happen when stuff is written to file, not afterwards.

Well, I tried the model example, but I didn’t see anything happen. I changed class NotePage to ProjectPage accordingly, and it lives here: /site/models/project.php

$props['content']['newTitle'] = $props['content']['title']

My blueprint looks like this:

titleFormat:
  type: textarea
  label: Title Format
  default: "{{ page.newTitle }}"

But, I didn’t see anything update.


So then I went back to hooks… set up a new plugin:

<?php

Kirby::plugin('ssk3-fields/titleformat', [
  'hooks' => [
    'page.create:after' => function ($page) {
      $page->update([
        'autoTitle' => $page->title()->value()
      ]);
    },
    'page.update:after' => function ($newPage, $oldPage) {
      $newPage->update([
        'autoTitle' => $newPage->title()->value()
      ]);
    },
    'page.changeTitle:after' => function ($newPage, $oldPage) {
      $newPage->save([
        'autoTitle' => $newPage->title()->value()
      ]);
    }
  ]
]);

And update my blueprint like so:

titleFormat:
  type: textarea
  label: Title Format
  default: "{{ page.autoTitle }}"

Wouldn’t page.autoTitle get updated on any of these 3 hooks?

Because your field name is titleformat, settting the title to the title value doesn’t make sense. And the default value in the blueprint doesn’t make sense either in this case.

As I already mentioned in the other tthead, a default value only works at page creation and since we know that the query string doesn’t worrk anyway, you can remove the default value altogether.

Sorry! I had a typo, I just edited my model example with ‘newTitle’

So, with the model, the blueprint and the hook in place, you will now end up with three differently named fields: newtitle, autotitle and titleformat, hurray!

What’s the name of the blueprint?

Blueprint is project.yml

Ok, then project.php model is at least correct.

Haha yes! (pumps fist)

So if we go with model, and not the hooks, how would I extend the model to drop in the title?

Should be

$props['content']['titleformat'] = $props['content']['title'];

Closer…warmer… hah. So I have this for my full extended model:

<?php

class ProjectPage extends Page {
  public static function create(array $props): Page {
    $props['content']['titleformat'] = $props['title'];
    return parent::create($props);
  }
}

and the blueprint is like so - omit the default prop now, correct?

titleFormat:
  type: textarea
  label: Title Format

But when I create a new project, the textarea field is still empty.

I corrected my suggestion

<?php

class ProjectPage extends Page {
  public static function create(array $props): Page {
    $props['content']['titleformat'] = $props['content']['title'];
    return parent::create($props);
  }
}

Hmm, still isn’t working. I read somewhere parent is no longer what the page model is extending? So is this still correct in the model? return parent::create($props);

No, what you read is that the model must refer to the page itself, not the parent page like before 3.4 (but that has nothing to do with calling parent::create.

This is what you are referring to, I guess: Hook page.create:before, how to edit user input?

Unless you are on an older Kirby 3 version, in which case you would have to put the create method in the parent page model…

Right, ha, that’s what I was referring to.

So… apparently I am running Kirby 3.3.5 (I just used the Kirby Vue Starterkit, I just assumed I was on the latest version since I started this project the other week).

Knowing I’m on 3.3.5, what should I do? I’m not using any hooks, jus the model extended and blueprint.

/site/models/project.php

<?php

class ProjectPage extends Page {
  public static function create(array $props): Page {
    $props['content']['titleformat'] = $props['content']['title'];
    return parent::create($props);
  }
}

site/blueprints/pages/project.yml

columns:
  - width: 2/3
    sections:
      rightCol:
        type: fields
        fields:
          titleFormat:
            type: textarea
            label: Title Format

Do I need to call the model somewhere / somehow or does Kirby do this under the hood?