Closed Daandelange closed 2 years ago
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.
Hello, I'd like to re-open this tiny issue, I'm still running into it (while there are multiple ways of bypassing this).
So the purpose of my request is to make the structure-field
better re-useable. Unless you really need to use the user-provided attributes from the blueprint, I think it makes more sense to use $this->props['fields']
or $this->fields
instead of $this->attrs['fields']
so the StructureField is easier to extend, with less redondant code. (see snippet below)
If other people are running into this issue, the easiest way I found is to add a form
to your extended-structure-field's methods
. This will also ensure compatibility with current Kirby version (3.8.3) and before.
Kirby::plugin('kirbybugs/staticstructure', [
'extends' => 'structure',
'props' => [
// Make fields static or whatever
'fields' => function($fields=[]){
//$this->attrs['fields']=[...]; // Here you could eventually force the prop into the attrs so that the native methods.form picks the right one.
return [ 'test'=>['label'=>'TestField','name'=>'test', 'type'=>'text'] ];
},
],
'methods' => [
// Overrides structure-field::methods::form() which grabs the form from $this->attrs['fields'] which we can't override.
// Until https://github.com/getkirby/kirby/issues/4107 is merged, and after this will still be needed this for compatibility with older kirby versions.
'form' => function (array $values = []) {
return new Form([
'fields' => $this->fields, // <-- only line changed
'values' => $values,
'model' => $this->model
]);
},
],
]);
Description
The
structure
field itself works great. But extending it, some unexpected behaviour happens. Context: I've been extending astructure
field by overriding itsfields
prop to a custom value, to dynamically provide one column per language. Result: The structure throws an error because it tries to use thefields
from the blueprint [aka$field->attr['fields']
].Undefined index: fields in file: /starterkit/kirby/config/fields/structure.php line: 162
Expected behavior
That the underlying
structure
field is able to use my changedfields
prop. (Or rather expect thestructure
field not to have afields
prop, so I'm unable to over-ride it ? ).Notes
fields
prop is initially required (structure-field
), changing it to not required myself, I can expect it to crash. But relying on the prop instead of the blueprint attrs would make the structure field more re-useable.Solving
Changing
$this->attrs['fields']
to$this->fields
solves the issue inkirby/config/fields/structure.php
. https://github.com/getkirby/kirby/blob/351b6348348cf78aed9a65920358caf218473102/config/fields/structure.php#L163 Everywhere else, thestructure
field already uses the parsed props via$this->propName
.Only here the code directly uses the popName from the attrs. (is there a reason for this? do other fields have similar bottlenecks?) This solution would definitely make re-using the structure field easier.
To reproduce
Here I expect the structure field to use my provided fields.
structure
behave correctly. The field from the user blueprint is shown correctly.But I still expect to see the fields from my props.
structure.php
. Everything works as expected.Solution 2
(see in comment) Everything works as expected.Edit: Whoops, I pasted the "fields part" in the js snippet. The code should work now, with clarified instructions.