Add string to a "type: files" field via createChild

Hey all,

I am in the process of migrating content from a drupal-database to kirby, grabbing everything out of the database tables, and create the pages via ->createChild() in Kirby.

My last problem is the import of the PDF files that were uploaded in drupal.
I already built the function to grab the files from the url and put them in the folder thats being created.

What I am stuck with, is simply connecting the pdf file to the “type: files” field.

I can manually edit the .txt file and add “- filename.pdf” to the right files field (“typefilesfield”) and the file is perfectly preselected in the panel and also showing in the frontend.
But I can’t get this done automatically inside the ->createChild() function.

So my problem is:

All strings inside the ‘content’ => array get attached to the fields. Theres only a problem with the files field(‘typefilesfield’). With the following code it always stays empty and won’t receive the string.

I guess I can’t just simply add a string to the files field in this way, but I can’t wrap my head around how to write it.

Thanks so much in advance for everyone thats trying to help.
Daniel

PHP:

$article = page('db-test')->createChild([
    'slug'      => 'some slug here',
    'template'  => 'the template name',
    'content'   => [
        'fileurl'        => 'fileurlhere',
        'title'          => 'titlehere',
        'typefilesfield' => '- filename.pdf'
    ]
]);

YML:

typefilesfield:
    label: Type Files Field
    type: files

The files field needs the data in yaml encode format Data::encode('fileid', 'yaml').

Also, it does not store a file URL, but the file id, which is the filename if the file lives in the same page.

1 Like

Thank you very much @pixelijn , thats a good tip.

In the meantime I solved my problem by not adding the file with the create function, but updating the $article after everything is processed:

$article->update([
    'typefilesfield'   => '- filename.pdf'
]);

This works somehow, so I think it has something to do with the order I am processing stuff… :thinking:

Thanks for your help.

It would still make more sense to yaml encode your file name, because this string syntax will only work for a single file.

$filenames = [
  'filea.pdf', 'fileb.pdf',
];

$filenames = Data::encode($filenames, 'yaml');

$article->update([
    'typefilesfield'   => $filenames,
]);
1 Like

Yes, I already did that :slightly_smiling_face:
Thanks!