Can anyone help me get a single image/media field working within a json repeater ?

GitHub
Getting an image field to work within a block repeater · area17 twi...
I think I'm very close to having this working but just need a bit of guidance. I'm using twill 2.x and I've read the discussion here on doing this as well as reviewing the default-confi...
13 Replies
ifox
ifox8mo ago
Thanks for detailing the issue and sharing your code, however we've indicated multiple times that medias fields are not supported in JSON repeaters, because it does not allow us to maintain integrity of the relationship with the media. I'm not sure I understand why you are not using a standard hasMany relationship to store the repeater, which would allow you to use any field without any customization you could also, even simpler, use a block editor field with a single block
AdamCord
AdamCord8mo ago
I may not actually be using json repeaters to tell you the truth. its code i was suggested to use some years back (i think by one of your team) on the old twill forum. basically i have a block defined in twill.php 'block_editor' => [ 'repeaters' => [ 'rate' => [ 'title' => 'Room Type', 'trigger' => 'Add room type', 'component' => 'a17-block-rate' ] ] ], my intention is simply to have a repeater element with multiple fields inside of it, and it's worked with this mechanism fine with various field types but now i need to add a single image field to each entry in the repeater
ifox
ifox8mo ago
I see, you'd need to migrate the data if you changed the approach since you already have content in that repeater I'll review your code to see if we can make it work
AdamCord
AdamCord5mo ago
I should clarify, I haven't changed approach. Since the start I had this block editor setup in the twill config and that relates to the other code I posted on github. I may just be using the wrong terminology due to confusion with the functionality/things I've read. So naturally I'd like a solution that continues with the approach to avoid data loss of existing data. As I say in the github post, I'm very close to getting it working, I can make the values save to the json in my table fine... I believe that it's simply a case that the array I return via the repository isn't of the correct format for twill to setup the media image field in the form as already defined/populated. hi there is there anything you can suggest on this? I must be so close to getting it working as it's only the reading back of the saved data into the edit form that doesn't work Is there any chance of reviewing this briefly? I've updated the git hub to make even clearer as to where I am with this. any change of assisting with this still? I'm desperate to get a fix to this and it seems to be something not document anywhere. 🙏
ifox
ifox5mo ago
Hi @AdamCord, as I've repeated multiple times, a repeater field saved inside a JSON column of a model DOES NOT support medias fields. I understand you want it to work, but why would it be documented anywhere if it is not supported? To give further detail on why: twill media library protects users from deleting images that are attached to some content. For that to be possible, a row needs to exists in the mediables table. If you save a media id inside your JSON column instead of respecting Twill's approach to medias fields relationships, twill wouldn't know that the image is used anywhere. I'm sure you don't care and still want that to work, but I wanted to share some reasoning. There is never any data loss in Twill. if you are switching to a different data modeling structure you can always migrate the data programmatically by looking at the before and after and writing a simple script. now, I hear that you feel close, and I can try to help, but please consider why it is not supported and that there are a lot of ways to resolve your problem instead of trying to add support for something that shouldn't really be supported. can you please provide a reproduction repository? You can share your repo privately with me on GitHub if you don't have time to extract the code.
AdamCord
AdamCord5mo ago
thanks for the response. Appologies I had missed the bit in your first response on this thread that it simply isn't supported. I had no idea about that. I wasn't trying to be unreasonable, I simply hadn't read that somehow. So your reasoning as to why it isn't supported makes sense although please understand that there is no reference to this in the documentation from what I see.. at least not when repeaters are discussed. So the assumption from my end was that repeaters would work with whatever standard twill field you add to them.
So I'm happy to provide a repository but to possibly save us both the time... can you just clarify what you are hinting at when you say that there are lots of other ways to resolve the problem? If there is a logical easy to use (from user's persective) work around more than happy to give it a go.
ifox
ifox5mo ago
Thanks for your understanding and sorry I had you waiting so long, I should have replied earlier. I found your SO post and felt bad tbh. Can you show me how you're currently using the repeater field? Not the inside of your repeater blade, but where you are using it.
AdamCord
AdamCord5mo ago
I'm not using it in the block editor @ifox .... It's just in a very standard edit page that outside of the repeater has very basic fields. Does that answer your question ?
Hazarcan Doğa
Hazarcan Doğa5mo ago
@ifox Hi, is it in your workflow to make media available for JSON repeaters?
ifox
ifox5mo ago
We will be looking into this now that 3.1.0 is out, but as I explained before, medias fields are not supported in JSON repeaters because we don't want to store direct references to medias and pivot crop information inside of a JSON column of your model. It would prevent keeping control over which medias are in use. I have an idea to make it work, but I recommend switching to a block editor. You can use multiple block editors in the same form, and you can allow a single block in a block editor field, which effectively would be a repeater, but it supports all fields, and infinite nesting.
AdamCord
AdamCord5mo ago
@ifox you mentioned various possible workarounds, so the recommendation is to use the block editor. any others just so I know ?
ifox
ifox5mo ago
Using a repeater that saves into a separate module with a hasMany/belongsTo relationship, and using $repeaters in the repository class, instead of $jsonRepeaters
AdamCord
AdamCord5mo ago
@ifox for future projects that is easy. the issue i have is that i have many records with existing repeater fields that work fine but now just wanted to add an image field as an extra. so basically no painless work around, I'd need to write some scripts to export the data in json and then store it differently in the relationship tables ?