I want to set the Month number in a field on page creation.
At first I tried with the page.create:after hook but all sorts of weird things where happening when I did…
Now I’m trying with a page model but no luck either, I don’t see what I’m doing wrong though. Probably something stupid
I have a contract.yml and my page model is /site/models/contract.php
class ContractPage extends Page {
public static function create(array $props): Page
{
$props['content']['renewalMonth'] = date('m');
return parent::create($props);
}
}
What exactly does not work? Does the field remain empty in a newly created page? For it to show up in the Panel, you have to define this field in the corresponding blueprint.
I was just reading about the page models and I’m wondering if I’m using it wrong. ( I do believe so actually ) – I don’t have a template, i just have the blueprint. My idea was to use as a create page hook -ish kinda thing but maybe that’s not what it’s supposed for …
If my suspection is right … is there a way to populate a page field in the page.create:after hook or something? (that was I was trying at first but no luck…)
many classes in kirby are sort of immutable. By that I mean that methods create and return a copy of the object and don’t necessarily “change” the object.
Using a model or a hook is not even necessary, because you can set a default in your blueprint, which is certainly the best option:
renewalMonth:
type: text
default: date('m')
But @rasteiner is right about the immutability of objects in Kirby, you always need to store the result of a method like update etc. in a variable in order to continue to work with that object.
Ah sorry, yes, I forgot to disable the model, that’s why I thought it worked Wishful thinking, because query language for defaults is what I would like to happen.