Spatie Media Library set alt text

I'm trying to set the alt text of an image in a form. I tried it using the custom properties, with a property 'alt_text'. It works when creating a new item, but I can't make an edit work.

Is setting the alt of an image using a custom property the way to go? Are there any alternatives?

See my code below: As long as the image not changes, the alt_text does not update. Even if the alt_text_input differs from the current custom property alt_text value.

public static function form(Form $form): Form
    {
        return $form
            ->schema([
                ...
                TextInput::make('alt_text_input')
                    ->live()
                    ->reactive()
                    ->required(),
                SpatieMediaLibraryFileUpload::make('image')
                    ->collection('bar-images')
                    ->imageEditor()
                    ->customProperties(function (Get $get) {
                        return ['alt_text' => $get('alt_text_input')];
                    })
                    ->afterStateHydrated(function (Get $get, Set $set, $state, $record) {
                        if( is_null($get('alt_text_input'))) {
                            if($record) {
                                if($record->getMedia('bar-images')->first()){
                                    $set('alt_text_input', $record->getMedia("bar-images")->first()->custom_properties['alt_text']);
                                }
                            }
                        }
                    })
                    ->image(),
                ...
            ]);
    }
Was this page helpful?