Changing page status makes my file unreachable from my field?

Hello everyone,

I’m facing a strange behaviour. For some context, i’m creating factories for my project in order to seed my content folder to reach different application states.

For one of my pages, i have a dedicated files field with a custom template

File: fields/document.yml

type: files
multiple: false
query: page.files.template('document')
layout: cardlets
label: Document chargé
uploads:
  template: document

And in my page blueprint, i have this field linked as below:

    fields:
      document: fields/document

So, in my factory, i create a file and then attach it to my page:

$file = File::create([
      'template' => 'document',
      'source'   => $tmpFile,
      'parent'   => $page,
      'filename' => basename($tmpFile),
      'mime' => $type === 'image' ? 'image/jpeg' : 'application/pdf',
  ]);

  $page = $page->update([
      'document' => $file->filename(),
  ]);

All of this works well, the page and file gets created and i can access the file trough the document field. But as soon as i run :

$page = $page->changeStatus('listed');

Now, the only thing that has changed in my content folder is the 1_ prefix on my page, but i cannot get my file trough the document field anymore (it is now set to ""). I’ve tried to fetch the page once again but it changes nothing.

And if i run the changeStatus method before creating the file, then it works all the way trough. I need to understand that behaviour as i fear that i’m doing something wrong that will backslash later.

Thanks a lot!
Sean.

The files field expects data as a yaml array, so you need to pass an array. Also, by default, it stores file uuids, so an entry for a single file in a files field looks like this:

Cover: - file://Jc0l1BA0i5z0QjRf

So when updating the files field, you need to pass an array of file uuids.

$page = $page->update([
      'document' => [$file->uuid()->toString()],
  ]);

Thank you for this correction, i made the modification but the issue is still there. When i use xdebug to go line by line, it’s really when it executes the changeStatus method that makes the page document attribute to be reset to "".

I did also checked the value stored in my ressource.txt. file and it has the right storage format (i checked before and after the correction, the format is good either way).

Tell me if you need anything that could help reproduce this bug, i can maybe make a simple reproducible code snippet.

Yes, or better still a simple starter or plainkit that includes the code to reproduce the issue. You can send me a download link via PM.