F
Filamentβ€’15mo ago
richeklein

Managing custom properties with Spatie Media Library plugin

Is there an easy way to store image meta data (e.g. descriptions for alt tags) when using the Spatie Media Library plugin? It seems like a custom property would work best. I see that you can add custom properties when uploading an image (https://filamentphp.com/docs/2.x/spatie-laravel-media-library-plugin/form-components#adding-custom-properties), but how do you manage the custom properties with a TextInput? Thanks.
Filament
Form components - Spatie Media Library Plugin - Filament
A plugin to add support for spatie/laravel-medialibrary to Filament.
9 Replies
Dan Harrin
Dan Harrinβ€’15mo ago
->customProperties(fn ($get) => ['property_name' => $get('text_input_name')]) or even using a keyval ->customProperties(fn ($get) => $get('keyval_name'))
richeklein
richekleinβ€’15mo ago
That works great! Thanks! Is there a simple way to update a custom property at a later time based upon a change in a field. For example, if an admin decides to change the image description, by updating the 'image_description" TextInput field, how can you trigger an update to the 'description' custom property for that media item? Thanks.
toeknee
toekneeβ€’15mo ago
afterStateUpdated on the image description and use fn($set('keyval_name', $get('text_input_name')) or similar should do it
richeklein
richekleinβ€’15mo ago
I see how that would work for standard form elements, but how do you access the actual media item from the callback to set a custom property using the customProperties() method on the SpatieMediaLibraryFileUpload element? This would happen after the image is already uploaded and now the admin is editing image description. Thanks again for your help.
Dan Harrin
Dan Harrinβ€’15mo ago
i dont think thats possible with media library you can only set the custom properties when the file is being uploaded you'd need separate logic to do that after it is uploaded like in the afterSave() of the admin panel, for example
richeklein
richekleinβ€’15mo ago
Makes sense. I didn't think it was possible but was hoping it was just my inexperience. πŸ˜€ Since I only have one photo per resource, I'll just maintain it as a separate field in the table. Not perfect, but simple!
SLy
SLyβ€’9mo ago
How to achieve this inside a multiple upload like: Forms\Components\SpatieMediaLibraryFileUpload::make('image') ->collection('gallery') ->imageEditor() ->multiple() ->image() ->downloadable() ->reorderable() I want to add a custom text field next to each image.
tg
tgβ€’9mo ago
I have the same requirement as SLy: I have a Media Library field allowing multiple uploads to a PDF collection, and I need to manage each file's type metadata which is stored in a custom property. I've experimented with a Repeater field as described by @Mansoor Khan in https://discord.com/channels/883083792112300104/1158821463168659496/1158822746457919498, but this would require overriding a lot of the callbacks of SpatieMediaLibraryFileUpload as they expect a Model implementing HasMedia instead of a single Media model. Is there a convenient way that I missed, or is a custom field the way to go?
Discord
Discord - A New Way to Chat with Friends & Communities
Discord is the easiest way to communicate over voice, video, and text. Chat, hang out, and stay close with your friends and communities.
Mansoor Khan
Mansoor Khanβ€’9mo ago
@tg I had to do good chunk hacks to make it work. It works as expected for the requirements but no a good approach. The final version is much more different.