I'm using twill 2.x and I've got a repeater element working within the editor fine. The repeater uses checkboxes and text fields and it writes the results of all the fields in the repeater into a json array that are then written into a single text field of the database. When the editor page loads another helper function decodes the json. This works fine.
The issue is that I now need to add a single image field to the repeater and it doesn't work. I'm very close to getting it to work. I have the data saving into the json array, it's just how to retrieve it and present it to twill in a method that it can understand to render the image field as already populated that I can't do. Right now, whatever I try, the edit page never recognises the media field values and it always shows the image field as not completed.
I have the block repeater setup like this in twill.php
'block_editor' => [
'repeaters' => [
'rate' => [
'title' => 'Room Type',
'trigger' => 'Add room type',
'component' => 'a17-block-rate',
],
],
],
I then have a template file at /blocks/rate.blade.php which defines the fields that are to appear in the repeater. To that working file I've now added:
@formField('medias', [
'name' => 'image_room_featured',
'label' => 'Featured image for room type',
])
In the repository file for the object I have these functions to save/load the data into the Twill edit page...
// prep the rates fields in repeater for saving
public function prepareFieldsBeforeSave($object, $fields) {
if (isset($fields['repeaters']['rate'])) {
$fields['rates'] = $fields['repeaters']['rate']; // save the repeaters value into property rates field array val
}
return parent::prepareFieldsBeforeSave($object, $fields);
}
// get data to display in the browser form field on page load
public function getFormFields($object) {
$fields = parent::getFormFields($object);
// rates
if (isset($fields['rates']) && !empty($fields['rates'])) {
// $fields, repeater name (as defined in master view file), the array containing values (casted in model file)
$fields = $this->getJsonRepeater($fields, 'rate', 'rates');
}
return $fields;
}
// function to handle conversion of json data to array, called by getFormFields()
public function getJsonRepeater($fields, $repeaterName, $serializedData) {
$repeater_items = $fields[$serializedData]; //JSON converted to array using $casts in Model
$repeatersConfig = config('twill.block_editor.repeaters');
foreach($repeater_items as $index => $repeater_item) {
$id = $repeater_item['id'] ?? $index;
$repeaters[] = [
'id' => $id,
'type' => $repeatersConfig[$repeaterName]['component'],
'title' => $repeatersConfig[$repeaterName]['title'],
];
$repeater_fields = array_except($repeater_item, ['id', 'browsers', 'blocks']);
foreach($repeater_fields as $index => $repeater_field) {
$repeater_data[] = [
'name' => 'blocks[' . $id . '][' . $index . ']',
'value' => $repeater_field
];
}
}
$fields['repeaters'][$repeaterName] = $repeaters;
$fields['repeaterFields'][$repeaterName] = $repeater_data;
return $fields;
}
If I use dd() to print the contents of the returned object to screen, this is what I see for the array within the object which contains the media data that has been retrieved:
5 => array:2 [▼
"name" => "blocks[1568046411918][medias]"
"value" => array:1 [▼
"image_room_featured" => array:1 [▼
0 => array:14 [▼
"id" => 1763
"name" => "_small_34.jpg"
"thumbnail" => "http://localhost:8001/img/2bfecead-6be5-4371-9f3f-f5900f7b5240/-small-34.jpg?fm=jpg&q=60&fit=max&dpr=1&h=256"
"original" => "http://localhost:8001/img/2bfecead-6be5-4371-9f3f-f5900f7b5240/-small-34.jpg"
"medium" => "http://localhost:8001/img/2bfecead-6be5-4371-9f3f-f5900f7b5240/-small-34.jpg?fm=jpg&q=80&fit=max&h=430"
"width" => 2048
"height" => 1365
"tags" => []
"deleteUrl" => null
"updateUrl" => "http://localhost:8001/admin/media-library/medias/single-update"
"updateBulkUrl" => "http://localhost:8001/admin/media-library/medias/bulk-update"
"deleteBulkUrl" => "http://localhost:8001/admin/media-library/medias/bulk-delete"
"metadatas" => array:2 [▶]
"disabled" => false
]
]
]
]
I think I'm very close to having this working but just need a bit of guidance.
BTW yes I have already asked on both the Twill Discord chat and their forum but no answer, there is very little support provided for the CMS it seems. I'm hoping someone at SO can save the day.