getkirby / kirby

Kirby's core application folder
https://getkirby.com
Other
1.32k stars 168 forks source link

Whitespace of YAML-encoded fields changes on save #4505

Closed hdodov closed 2 years ago

hdodov commented 2 years ago

Description

When you save a YAML-encoded field in the panel (e.g. a files field) and it has one entry, it's saved like this:

Logo:

- image.png

Later, if you use $model->writeContent(), it turns to this:

Logo: - image.png

This makes it annoying when you keep changes in Git, because you get "fake" changes.


The first format happens because when the field value is encoded with YAML here:

https://github.com/getkirby/kirby/blob/cfac315bc685193d0fe1b10fe3bf45a96e48fd15/src/Data/Yaml.php#L37

…it is returned with a newline character at the end, i.e.:

- image.png\n

Then, when it's saved by the TXT data handler, it's considered a multi-line string and has \n\n prepended to it:

https://github.com/getkirby/kirby/blob/cfac315bc685193d0fe1b10fe3bf45a96e48fd15/src/Data/Txt.php#L76-L79

The second format happens because the TXT data handler trims whitespace from the value here:

https://github.com/getkirby/kirby/blob/cfac315bc685193d0fe1b10fe3bf45a96e48fd15/src/Data/Txt.php#L125

So what was previously - image.png\n is now - image.png. Later, when it's saved, the value is no longer considered multi-line and does not get \n\n prepended to it.

Expected behavior
The first format should never happen, because the string is essentially one line. When saved from the panel, the field should look like this:

Logo: - image.png

To reproduce

  1. Create a site.yml blueprint with a pages field

  2. Save some value through the panel to get the first format

  3. Execute this:

    kirby()->impersonate('kirby');
    
    $data = site()->content()->toArray();
    $data['title'] = time();
    
    site()->writeContent($data);
  4. You'll see the second format

Your setup

Kirby Version
3.7.1

lukasbestle commented 2 years ago