How can I set a file through the update method of a page ?
I tried these which doesn’t work in Kirby 3.2.0
(cover is a Files Field with multiple: false
and no specific template restriction)
$page->update([
'cover' => '- cover.jpg'
]);
$page->update([
'cover' => $page->file('cover.jpg')
]);
Maybe there is a validation somewhere ?
You would have to store the file id, not the file itself.
if($cover = $page->file('cover.jpg')) {
$page->update([
'cover' => $cover->id()
]);
}
Thanks but this doesn’t work either…
Do you authenticate anywhere? Do you get an error message? Always use a try-catch
block
This function can only be called if you are logged in and I use kirby()->impersonate('kirby')
before.
I tried this and everything goes well, except the file which is still not stored in the field/content.txt
if($cover = $page->file('cover.jpg')) {
try {
$page->update([
'cover' => $cover->id()
]);
} catch (Exception $e) {
echo $e->getMessage();
}
}
After a few tests it seems to work with $cover->id()
Thanks a lot for your help @texnixe
Other solution by @texnixe :
Page::create([
'filesField' => Data::encode(['image.jpg'], 'yaml')
])