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:
- I have installed a fresh Kirby 3.3.2 Starterkit.
- 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
- 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
]);
};
}
],
]);
- 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).