Page Titles that are a date

I have a case where we want the page title to be a date (YYYY-MM-DD). But the following problems arise:

  • The page is immediately set to visible
  • The year is cut off from the url. (eg: 2017-02-09 -> […]/02-09)

Any idea how to fix this?

Options:

  • use a date without dashes
  • use another title and use a second field for the date, unless you need the date in the url
  • use a non-number prefix

Here’s what I did on a recent project with the same requirement. I omitted the title on the blueprint and used a date field to automatically fill the title field (which is required by Kirby) on each update.

#blueprints/article.yml

fields:
  # title:
  #   label: Title
  #   type: title
  #   required: true
  #   width: 3/4

  date:
    label: Publish Date
    type: date
    default: today
    format: MMMM D, YYYY
    required: true

Then I added a panel.page.update hook:

<?php // site/plugins/hooks.php

/*
 * Update issues title based on the
 * publish date field
 */
$kirby->set('hook', 'panel.page.update', function ($page) {
  if ($page->intendedTemplate() === 'article') {
    $title = $page->date('F j, Y');
    $uri = str::slug($title);

    // Stop if there's a sibling with same date
    if (! $page->parent()->find($uri)) {
      // otherwise update title and URI
      $page->update(compact('title'));
      $page->move($uri);
    }
  }
});

When the page is created you can type anything on the title, after the first save both title and URI will always reflect the date field.

Adjust the date formats to fit your requirements, just keep in mind that the format on the blueprint is moment.js format and the one on the hook is PHP date format

2 Likes

With your date format, @pedroborges, there is no problem with that. But if you use a data format like “2017-23-01” you will definitely run into issues.

Got it, in that case I’d use a date format that don’t causes sorting issues and create a Page model to customize the title.

<?php site/models/article.php

<?php

class Articlepage extends Page
{
      public function title()
      {
          return $this->date('Y-m-d');
      }
}

1 Like

I seem to get this behaviour whenever my title starts with a number. :thinking: So even if I initially use “9. Februar 2017” I get the exact same behaviour.

The problem is that if you have a title like this, the dot is converted to a dash in the page UID.

Do you need these pages to be invisible? After they are visible the issue is gone.

If you don’t need that distinction you can add a hook to make them visible when created.

Yup.

Basically it’s a list of events which each can have as little information as just a date and none of the events have a title – hence I used the title as the date-input. Most of the information comes from the parent page unless overwritten. And my client decides to publish the event, it should remain hidden – and the date must be in the URL.

Not really sure rn how to approach this. I didn’t notice this behaviour until recently.

Hm, I tried “12 reason to test this” as a title, and I still got the same behaviour. I suspect that this behaviour is not desired.

You just can’t have a uid/title that starts with a number unless it’s only a number, like “1984”.

Okay,

In that caste I will just create a custom route, use a custom date-field and populate it via a hook, like pedroborges showed. :slight_smile:

Thanks!

i am trying to achieve something similar, that is changing the page title in an page.update-hook. it technically works, but i only get to see the new title in the panel after i manually reload the page.

what is the way to go here? i tried

  • page.update:before/after and call page->changeTitle()
  • page.update:before/after and call page->update()

but none of those methods updates the value in the panel…

You can override the create method in a page model rather than using a hook. No page reload necessary.

( Please don’t post Kirby 3 related stuff in an old Kirby 2 thread, it’s a bit irritating).

1 Like