Hello,
v4.5 here.
I’ve been programmatically updating files fields, and I am having a brain-breaking problem.
I have pages with a files field that:
- Shows visually EMPTY in the panel.
- Is NOT EMPTY in the content file.
- Seems NOT EMPTY when programmatically checking it.
- The file it points at seems to exist when programmatically checking it.
This is the relevant bits of the blueprint declaration:
title: Publication
(...)
fields:
cover:
label: Cover Image
type: files
multiple: false
size: medium
layout: cards
translate: false
uploads: image
This is an example of how I updated the cover field:
$p->update([
"cover" => Data::encode([$filename], "yaml"),
]);
This is the relevant bits of content file for one of these pages:
Title: 1979 abril-maig
----
Cover: - file://ThGiumUCdevzznrJ
This is my debugging code:
$uuid = $p->cover()->yaml();
try {
$file = $p->cover()->toFile();
if ($file && $file->exists()) {
echo "✅ Cover file exists: “{$file->filename()}” (UUID: {$uuid[0]})<br>";
} else {
echo "❌ toFile() returned null (invalid UUID or no cover set) – stored value: “{$uuid[0]}”<br>";
}
}
This is the output for this particular page:
1979 abril-maig
✅ Cover file exists: “1979-guino-galeria-trece-web.jpg” (UUID: file://ThGiumUCdevzznrJ)
And this is how it looks in the panel, notice the apparently empty “cover” files field:
If I click on the field, the image shows with the right title:
Aaand, this happens in a LOT of these pages.
Help?
Thanks 
I ran some additional tests.
Apparently, if I select the file manually in the panel, then the content file shows:
Cover: - file://R0GMZtJq4lC2X9Sq
Which does NOT match the UUIID shown before which was:
✅ Cover file exists: “1979-guino-galeria-trece-web.jpg” (UUID: file://ThGiumUCdevzznrJ)
But how could this be? Considering I was extracting that UUID using:
$uuid = $p->cover()->yaml();
$file = $p->cover()->toFile();
echo "✅ Cover file exists: “{$file->filename()}” (UUID: {$uuid[0]})<br>";
And now it says:
✅ Cover file exists: “1979-guino-galeria-trece-web.jpg” (UUID: file://R0GMZtJq4lC2X9Sq)
Even more confused now.
I guess R0GMZtJq4lC2X9Sq is then the correct uuid. Not quite clear where the other one comes from, particularly since the code to update the page you posted above is very rudimentary. Does the first UUID ThGiumUCdevzznrJ exist anywhere?
Thank you @texnixe
I took the easy option, ignored mismatched outputs, and just bruteforced the right files into the field.
When you say the code is rudimentary, I assume you meant just basic; or did you mean that the code may be wrong or perhaps that there is a better, reccomended, way of updating?
I am, at the moment, attempting to update a pages field, multiple pages, programmatically.
I have an array of uuids of pages, and I am yaml encoding them as such:
$artists = Data::encode($a, 'yaml');
dump($artists);
which outputs:
- page://IFoC60wFgNzv5G1D
- page://kupN2lsY95eCgFRz
Can i then just do?:
$mypage->update([
'artists' => $artists
]);
Thank you.
What I meant is that from this piece of code it is not clear what you are doing here, because I do not see how $filename is defined or where this variable is coming from.
Now your second example is just as incomplete, and completely different.
Please post complete code that can be fully reproduced.
Well, it is frankly convoluted code that takes data from a csv and prepares it to be ingested by Kirby’s fields. But simplifying:
$shop = $site->csvToArray(asset("assets/csv/shop.csv")->url());
foreach ($shop as $s) {
$title = $s["Title"];
$slug = str::slug($title);
$artists = $s["Artists"];
// Content of $artists at this point: ["Bloom, Barbara; Lawler, Louise"]
// I leave out some code here to turn strings of artists' names in $artists into an array of uuids of already existing 'artist' pages
$artists = Data::encode($artists, 'yaml');
// Content of $artists at this point:
// - page://IFoC60wFgNzv5G1D
// - page://kupN2lsY95eCgFRz
if (page('publications')->findPageOrDraft($slug)) {
try {
$p->update([
'artists' => $artists,
]);
} catch (Exception $e) {
echo $e->getMessage() . '<br />';
}
}
}
Let me know if any other bit of the code would be necessary, please. Thanks.
Ok, so far so good, but what is the result? Is the data correctly stored in the artists pages field?
I haven’t run this code, my question was if that was the recommendable way of updating a page, but from your answer it would seem so.
$p is not defined here, so the correct code would be
if ($p = page('publications')->findPageOrDraft($slug)) {
try {
$p->update([
'artists' => $artists,
]);
} catch (Exception $e) {
echo $e->getMessage() . '<br />';
}
}
1 Like