TimOetting / kirby-v3-builder

17 stars 2 forks source link

Field type "file" is not save correctly #10

Closed thomasfahrland closed 5 years ago

thomasfahrland commented 5 years ago

When saving a build section, the content of a file field is not stored in the content txt file as in Kirby standard. Actually this plugin is saving many other infos instead of only the filename.

.yml config: config.yml.txt

Result: result

sylvainjule commented 5 years ago

This is also applicable to the Pages field.

From what I get the builder is grabbing the value of vue components within its blocks and saves it directly, whereas Kirby's update process has been refactored and now checks the field's id against its stored value, stored in $store.state.form.models['page-id'].values, and saves only parts of it if it has changed.

I believe it started with this commit but I'm not sure where to pinpoint this exactly, better ask @distantnative.

Files fields store an array of images' filename:

myimages:
  - image-01.jpg
  - image-02.jpg

Pages fields store an array of pages' id:

mypages:
  - path/to/page
  - path/to/another-page
sylvainjule commented 5 years ago

To get around it for now when using the field in your templates you can add some fieldMethods setting the array correctly before converting the fields:

'fieldMethods' => [
    'toBuilderFiles' => function($field) {
        if(is_array($field->value())) {
            $ids = array_map(function($field) {
                return $field['filename'];
            }, $field->value());
            $field->value = Yaml::encode($ids);
        }
        return $field;
    },
    'toBuilderPages' => function($field) {
        if(is_array($field->value())) {
            $ids = array_map(function($field) {
                return $field['id'];
            }, $field->value());
            $field->value = Yaml::encode($ids);
        }
        return $field;
    }
],

And then use $filesField->toBuilderFiles()->toFiles(). This way when it gets fixed, you'll only have to delete the method part.

thomasfahrland commented 5 years ago

sylvainjule thanks for the workaround, works! :)

TimOetting commented 5 years ago

The current commit fixed this issue. Files and pages should now be saved in the correct structure