HowTo: Automatic update of the 'last_updated' field in the Panel

According to https://forum.getkirby.com/t/blueprint-date-k2-override-behavior/12530/3 and https://forum.getkirby.com/t/blueprint-date-k2-override-behavior/12530/5 I insert two automatically updated fields “last_updated” and “last_updated_by” into a page blueprint.

HowTo
Steps to reproduce:

  1. I have installed a fresh Kirby 3.3.2 Starterkit.
  2. I added the following page blueprint “legal.yml”:
# site\blueprints\pages\legal.yml
title: Legal
columns:
  - width: 1/3
    sections:
      pages:
        type: pages
        headline: Pages
        create:
          - legal
      files:
        type: files
        headline: Files
  - width: 2/3
    sections:
      content:
        type: fields
        fields:
          created:
            # as draft
            label: Created on
            type: date
            time: true
            default: now
            disabled: true
            width: 1/2
          created_by:
            label: Created by
            type: users
            multiple: false
            disabled: true
            width: 1/2
          last_updated:
            label: Last updated on
            type: date
            time:
              step: 1
            disabled: true
            width: 1/2
          last_updated_by:
            label: Last updated by
            type: users
            multiple: false
            disabled: true
            width: 1/2
          text:
            label: Text
            type:  textarea
  1. I added a plugin with this file “site\plugins\kirbyforum_lastupdated\index.php”:
<?php // https://forum.getkirby.com/t/blueprint-date-k2-override-behavior/12530/3
Kirby::plugin('kirbyforum/lastupdated', [
  'hooks' => [
    'page.update:after' => function ( $page ) {
      $mydate = date( 'Y-m-d H:i' );
      $myuser = kirby()->user();
      if( $page->last_updated()->exists() AND $page->last_updated_by()->exists()){
        $page->update([
          'last_updated' => $mydate,
          'last_updated_by' => $myuser
        ]);
      };
    }
  ],
]);
  1. I added a page “Legal 01” with the content file “legal.txt” using the Panel and opened it in the Panel. You need, like I did, to customize at least one blueprint page file or the site blueprint file of the Starterkit so that you can add this page in the Panel!
    Every “Save” of the page “Legal 01” updates these two fields in the content file of the page, but NOT these fields in the Panel!

Kirby Version
Tested with Starterkit 3.3.2

Remarks

  • Many thanks to @texnixe for her help (see the following articles), without which I would not have been able to implement this solution.
  • Also many thanks to @ahmetbora for his help.
  • My thanks also go to @moritzebeling for the original contribution (links at the beginning of the first article of this page).
1 Like

Is the last_updated field exist in the legal.txt file before update the page?

Yes, but if I delete it in the content file before, nothing changes…

My impression is that the target formatting (with time !!!) is evaluated incorrectly. If I remove the time everywhere, the field is updated. But I need the time in that field!

The content file look like:

Title: Legal 01

----

Published: 2019-12-31 12:10

----

Published-by:

- 1mBwvBCJ

----

Last-updated: 2019-12-31 12:15

----

Last-updated-by:

- 1mBwvBCJ

----

Text: Test

The problem is probably that you want to update the page twice for no reason. Change your code logic so that you only have one update statement.

Or store the first update in a new variable, then in the second step, use that variable to update. But imho, it is a waste to perform the update twice. I’d even argue that it is not necessary to check if the fields exist, anyway. You could check for intended template instead, where you know these fields exist or should exist.

1 Like

I can’t reproduce the issue so last_updated updated success in legal.txt. But the field not updated in panel ui if you mean that?

Thank you both!

I have changed the code, please look above. Now the field changes date and time, but in steps of 5 minutes…

No, I know.
That may be a pity, but at the moment it probably cannot be changed.
More important is the change in the content, which is rendered by the template.
This works now in 5 minute steps…

That sounds like a bug.

You could use a text field instead, if you use a calendar icon and since the field is disabled anyway, it doesn’t really matter.

      last_updated:
            label: Last updated on
            type: text
            icon: calendar
            disabled: true

Great suggestion, that’s what I will do (and some more visual changes in the panel: e.g. 4x width: 1/2).

Thank you very much! :smiley: :smiley: :smiley:

Actually, it’s not a bug but a wrong blueprint setup, should be

          last_updated:
            label: Last updated on
            type: date
            time:
              step: 1
            disabled: true
1 Like