Last active
May 26, 2025 06:51
-
-
Save afsakar/4af14e790acda022952292beeef0df66 to your computer and use it in GitHub Desktop.
Edit custom properties action for SpatieMediaLibraryFileUpload component
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Filament\Actions; | |
use Spatie\MediaLibrary\MediaCollections\Models\Media; | |
use Closure; | |
use Filament\Forms; | |
use Filament\Notifications\Notification; | |
use Illuminate\Support\HtmlString; | |
class EditCustomMediaPropertyAction extends Forms\Components\Actions\Action | |
{ | |
public array $customComponents = []; | |
public static function getDefaultName(): ?string | |
{ | |
return 'edit_custom_media_property'; | |
} | |
protected function setUp(): void | |
{ | |
parent::setUp(); | |
$this->hiddenLabel() | |
->slideOver() | |
->hidden(fn ($state) => empty($state)) | |
->icon('heroicon-o-pencil-square') | |
->modalHeading(__('Edit Media Properties')) | |
->tooltip(__('Edit Media Properties')) | |
->modalWidth('2xl') | |
->modalSubmitAction(fn ($action) => $action->label('Kaydet')) | |
->mountUsing(function (Forms\Form $form, $component) { | |
$state = collect($component->getState()); | |
$isMultiple = $component->isMultiple(); | |
if ($isMultiple) { | |
$mediaCollection = Media::whereIn('uuid', $state->keys())->orderBy('order_column', 'desc')->get(); | |
$items = []; | |
foreach ($mediaCollection as $item) { | |
$items[] = [ | |
'uuid' => $item->uuid, | |
'name' => $item->name, | |
'original_url' => $item->original_url, | |
'custom_properties' => $item->custom_properties, | |
]; | |
} | |
$form->fill([ | |
'items' => $items, | |
]); | |
} else { | |
$media = Media::where('uuid', $state->first())->first(); | |
$form->fill([ | |
'uuid' => $media->uuid, | |
'name' => $media->name, | |
'original_url' => $media->original_url, | |
'custom_properties' => $media->custom_properties, | |
]); | |
} | |
}) | |
->form(function ($component) { | |
if (! $component instanceof \Filament\Forms\Components\SpatieMediaLibraryFileUpload) { | |
throw new \Exception(__('This action can only be used with SpatieMediaLibraryFileUpload component.')); | |
} | |
$isMultiple = $component->isMultiple(); | |
if ($isMultiple) { | |
return [ | |
Forms\Components\Repeater::make('items') | |
->hiddenLabel() | |
->collapsed() | |
->addable(false) | |
->deletable(false) | |
->reorderable(false) | |
->itemLabel(fn ($state) => $state['name'] ?? 'Medya') | |
->schema($this->getFormSchema()), | |
]; | |
} else { | |
return [ | |
Forms\Components\Group::make() | |
->schema($this->getFormSchema()), | |
]; | |
} | |
})->action($this->edit_property()); | |
} | |
public function edit_property(): Closure | |
{ | |
return function ($data, $component) { | |
$isMultiple = $component->isMultiple(); | |
try { | |
if ($isMultiple) { | |
$mediaCollection = Media::whereIn('uuid', collect($data['items'])->pluck('uuid')->toArray())->get(); | |
foreach ($mediaCollection as $media) { | |
$custom_properties = collect($data['items'])->where('uuid', $media->uuid)->first()['custom_properties']; | |
$media->name = collect($data['items'])->where('uuid', $media->uuid)->first()['name']; | |
if (isset($custom_properties)) { | |
$media->custom_properties = $custom_properties; | |
} | |
$media->save(); | |
} | |
} else { | |
$media = Media::where('uuid', $data['uuid'])->first(); | |
$media->name = $data['name']; | |
if (isset($data['custom_properties'])) { | |
$media->custom_properties = $data['custom_properties']; | |
} | |
$media->save(); | |
} | |
Notification::make('success') | |
->title(__('Success')) | |
->body(__('Media properties have been updated successfully.')) | |
->success() | |
->send(); | |
} catch (\Exception $e) { | |
Notification::make('error') | |
->title(__('Error')) | |
->body($e->getMessage()) | |
->danger() | |
->send(); | |
} | |
}; | |
} | |
public function action(Closure|string|null $action): static | |
{ | |
$this->action = $this->edit_property(); | |
return $this; | |
} | |
public function customComponents(array $components): self | |
{ | |
$this->customComponents = $components; | |
return $this; | |
} | |
protected function getFormSchema(): array | |
{ | |
return [ | |
Forms\Components\Hidden::make('uuid'), | |
Forms\Components\Placeholder::make('original_url') | |
->hiddenLabel() | |
->dehydrated(false) | |
->content(fn ($state) => new HtmlString('<img src="'.$state.'" class="object-cover rounded-lg">')), | |
Forms\Components\TextInput::make('name') | |
->label(__('Media Name')) | |
->required(), | |
Forms\Components\Section::make(__('Custom Properties')) | |
->statePath('custom_properties') | |
->translateLabel() | |
->collapsed() | |
->schema([ | |
...$this->customComponents, | |
])->visible(fn (): bool => ! empty($this->customComponents)), | |
]; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// example usage; | |
Forms\Components\SpatieMediaLibraryFileUpload::make('gallery') | |
->label('Gallery') | |
->image() | |
->multiple() // it's support multiple media also | |
->hintActions([ | |
EditCustomMediaPropertyAction::make() | |
->customComponents([ | |
Forms\Components\TextInput::make('caption') | |
->label('Caption') | |
->required(), | |
]), | |
]) | |
->reorderable() | |
->panelLayout('grid') | |
->collection('post_gallery') | |
->nullable(), |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment