Structure field—Default values

Hello,

I’m using Kirby 2.4.0, and I’m trying to create a Structure field to list product dimensions like width, length, and height, and sometimes other things like diameter, or depth.

I think that using the structured field might be a good way to present this to the editor. I’d like to have a few starting values to make product creation easier.

It seems like default values for Structure fields isn’t working. I’m testing with the code provided in the Cheatsheet (https://getkirby.com/docs/cheatsheet/panel-fields/structure):

emails: 
label: Emails
type: structure
default: 
  - email: bastian@getkirby.com
  - email: sascha@getkirby.com
  - email: nico@getkirby.com
fields: 
  email: 
    label: Email
    type: email

This does not seem to have any effect. I note that there’s a thread about this here, but there doesn’t seem to be a resolution: Problem with structure default values in Blueprint - #4 by texnixe

Any suggestions? Thank you!

Yes, this does not seem to work. You can set default values to individual fields of the structure field though.

Edit: I created an issue on GitHub.

Thank you texnixe! I hope the issue gets resolved.

If it helps anyone, my workaround was to create a tags field in the parent page, and put a select field in the child page. Here’s my code:

dimensions:
  label: Dimensions
  type: structure
  style: table
  fields:
    dimensionkey:
      label: Key
      type: select
      options: field
      field:
        page: ../
        name: dimensions
    dimensionvalue:
      label: Value
      type: text
1 Like

I think that solution makes more sense then prepopulating entries that noone needs in your use case.

you could use a hook to track page.create, check template and then add the default values to the structure.

1 Like

my blueprint

title: Expose
fields:
  title:
    label: Titel
    type:  text
  datenfelder:
    label: Tabelle
    type: structure
    entry: > 
      {{dftitle}} {{dftext}}
    default:
      -
        dftitle: Mietfläche 
        dftext: "7.000 {{m2}}"
      -
        dftitle: Grundriss
        dftext: " "
    fields:
      dftitle:
        label: Titel
        type: text
        required: true
      dftext:
        label: Text
        type: textarea
        buttons: false
        required: true

in my site.php

function blueprintStructureFromPage($page) {
	$yml = Yaml::decode(file_get_contents(kirby()->roots->blueprints() . DS . $page->template() . '.yml'));
	if(!$yml || count($yml) == 0) return null;

	$blueprint = new Structure($yml);
	$blueprint->set('fields', new Structure($blueprint->fields()));
	foreach ($blueprint->fields() as $fkey => $fval) {
		$blueprint->get('fields')->set($fkey, new Structure($fval));
            // NOTE: no more deeper nesting implemented
	}
	return $blueprint;
}

in my config.php

kirby()->hook('panel.page.create', function($page) {
  if($page->template() == 'expose') {
    $data = array();
    $blue = blueprintStructureFromPage($page);
    foreach ($blue->fields()->datenfelder()->default() as $default) {
      $a = print_r(a::get($default, 'dftitle'), true);
      $b = print_r(a::get($default, 'dftext'), true);
      if($a && $b) array_push($data, ['dftitle' => $a, 'dftext' => $b]);;
    }
    try { 
      $page->update(['datenfelder' => yaml::encode($data)]); 
    }
    catch(Exception $ex) {}
  }
});
1 Like

Hi, I am trying to implement bnomei hook, but with no success (totally new to this). Where exactly is site.php file located?

You can put the function into a file in site/plugins (e.g. functions.php), no need for a site.php (that file does not exist by default).

you could create it. https://getkirby.com/docs/developer-guide/configuration/folders

What is your reason for putting the function in the site.php instead of in the plugins folder, @bnomei?

i suppose i had a site.php in my projects before i had any functions.php in plugins folder. so i just kept using that. but plugins folder is a fine way to do it and @alberto should put it there like you suggested.

So basically the structure field doesn’t support default values:broken_heart:

specs:
  label: Table
  type: structure
  default:
    -
      title: Software
      description: 
    -
      title: Dimensions
      description: 
    -
      title: Weight
      description: 
  entry: >
    {{title}}
  modalsize: large
  fields:
    title:
      label: Title
      type: text
    description:
      label: Description
      type: textarea

the docs say default values are supported. hm. did not check that recently.

https://getkirby.com/docs/cheatsheet/panel-fields/structure

I did a test a couple of days ago and found that default values stopped working with Kirby 2.2…

I’ve tried the sample code with Kirby 2.5.1, and it looks like the default values are not working in this version either.

Well, yes, as I said above, it stopped working and the issue on GitHub is still open.

This feature will be re-implemented in K 2.5.3 and is already on the dev branch.

1 Like

Could it be that one year later, this doesn’t work any more?

I looked at code that fixed https://github.com/getkirby/panel/issues/956, and it’s not there any more.