Dynamic default number field

Hi,
In my siteMethods, I get a number that I’d like to use as a default for a field in my blueprint.

‘getSeason’ => function () {
… (some calculs)
$age = date(‘Y’) - $then_year;
return (int)$age;
},

And my blueprint is as simple as this :

season:
  type: number
  default: "{{ site.getSeason }}"

It works if the field in a text field (it has the value 76 as default), but not with a number type.
Is there a problem with my variable not being considered as a number?

Thank you and have a nice day.

String templates aka query language are not supported for most default values. What you could do is create your own number field in plugin:
PHP:

Kirby::plugin('my/plugin', [
	'siteMethods' => [
		'getSeason' => function () {
			return 5;
			},
	],
	'fields' => [
		'num' => [
			'extends' => 'number',
			'props' => [
				'default' => function ($default = null) {
					return $this->toNumber($this->model->toSafeString($default) ?? '');
				},
			]
		]
	]
]);

JS: index.js

panel.plugin("my/plugin", {
    fields: {
        num: {
            extends: "k-number-field"
        }
    }
});

Yaml:

    fields:
      season:
        type: num
        default: "{{ site.getSeason }}"

The above refers to Kirby 3 + 4, just noticed that according to the category you have chosen you still seem to be on Kirby 2, is that correct?

For Kirby 2, I can’t remember, it’s no longer supported.

It works like a charm, I need to dive into plugin creation…
Thanks for the help and the explanation !

dear sonja, somehow i am trying to extend the radio field to be able to user querylanguage for the “disabled” property.

my js

panel.plugin("squareclouds/customradio", {
    fields: {
        radio: {
            extends: "k-radio-field"
        }
    }
});

my index.php

<?php
Kirby::plugin('squareclouds/customradio', [
    'fields' => [
        'radio' => [
            'extends' => 'radio',
            'props' => [
                'disabled' => function ($disabled = null) {
                    if ($disabled !== true && $disabled !== false) { // <- if its other than true or false do the check of the user
                        if ($this->model->toSafeString($disabled) == "member")
                            return true; // <- hide if regular member
                        else
                            return false;
                    } else {
                        return $disabled;
                    }
                },
            ]
        ]
    ]
]);

the disabling is working fine, but somehow all my radios look empty.

should look like for instance here

but they all look like this

am i doing something wrong?

my js and php are in the same plugin folder

EDIT: BTW somehow the php was creating problems also in my select fields, so i had to comment it out for the time being. my selects in other parts of the panel were empty

As far as I know this only works properly if your custom field has its own name, so customradio or so

ah! thank you!! didnt realize vernone was also using an alternative name for the new filed. now everything works :slight_smile: