Hello there!
I’ve got an issue with Select fields when trying to creating programatically custom blocks for a page.
For some reasons, I want to populate some fields from an old Builder field to a new Block field. I followed this tutorial and I succeed to populate my new Block field with the data from the old Builder, except for select fields, where no data are added.
Here is the data of the old Builder field :
# content/some-member/member.txt
Builder:
-
nom: Movie Name
credits: John Doe
realisation: John Doe and Michael Bay
production: John Doe Production
annee: 2021-01-01
genre: Fiction
poster: ""
type: Long-Métrage
_fieldset: cinema
Here is the code I use to create the new block field from the Builder data:
# member.php
<?php
# I've added ->limit(1) only for testing purpose
foreach($page->builder()->toBuilderBlocks()->limit(1) as $oldBlock) {
$blocks = $page->filmographie()->toBlocks();
# Get all the values from the old Builder
$nom = $oldBlock->nom();
$credits = $oldBlock->credits();
$realisation = $oldBlock->realisation();
$production = $oldBlock->production();
$annee = $oldBlock->annee();
$genre = $oldBlock->genre();
$poster = $oldBlock->poster();
$format = $oldBlock->type();
$media = $oldBlock->_fieldset();
# Create new Block using the old values from builder
$newBlocks = new Kirby\Cms\Block([
'content' => [
# this one does not populate
'format' => $format,
# this one does not populate
'media' => $media,
'nom' => $nom,
'credits' => $credits,
'realisation' => $realisation,
'production' => $production,
'annee' => $annee,
# this one does not populate
'genre' => $genre,
'poster' => $poster,
],
# custom block type
'type' => 'project',
]);
$blocks = $blocks->add(new Kirby\Cms\Blocks([$newBlocks]));
$kirby->impersonate('kirby');
if($page->filmographie()->toBlocks()->isEmpty()) {
$page->update([
'filmographie' => $blocks->toArray(),
]);
}
}
?>
And this is what is populated to the new Block field:
Filmographie:
[
{
"content": {
"format": "",
"media": "",
"nom": "Movie Name",
"credits": "John Doe",
"realisation": "John Doe and Michael Bay",
"production": "John Doe Production ",
"annee": "2021-01-01",
"genre": "",
"poster": []
},
"id": "0aecdcbc-8d36-4173-9425-2f9154b0d1aa",
"isHidden": false,
"type": "project"
}
]
As you can see, format
, media
, and genre
(all select field type) are empty while other fields are populated. Do you have any idea what I’m doing wrong here?
Thank you very much for any help!
(using Kirby 3.5.5)