How to safely override Filament core components methods?

Hi everyone! I’ve made a couple of small modifications directly to Filament’s core BaseFileUpload class (in vendor/filament/filament/src/Forms/Components/BaseFileUpload.php), specifically in two methods, removeUploadedFile(string $fileKey) and reorderUploadedFiles(array $fileKeys), adding this line: $this->callAfterStateUpdated(); I added this call to callAfterStateUpdated() so that my additional logic inside every FileUpload->afterStateUpdated() can run. The problem is that these changes will be lost if I update or reinstall my Composer dependencies. So, what’s the recommended way to override or hook into these methods without directly modifying Filament core files? Thank you in advance!
Solution:
Hello You can Create a new class that extends BaseFileUpload and override the methods up there ... ``` class CustomFileUpload extends BaseFileUpload...
Jump to solution
2 Replies
Solution
H4L1M
H4L1M5w ago
Hello You can Create a new class that extends BaseFileUpload and override the methods up there ...
class CustomFileUpload extends BaseFileUpload
{
protected function removeUploadedFile(string $fileKey): void
{
.....
}
class CustomFileUpload extends BaseFileUpload
{
protected function removeUploadedFile(string $fileKey): void
{
.....
}
Then use it
steexd
steexdOP5w ago
Yup, I did it this way and it works:
<?php

namespace App\Filament\Forms\Components;

use Filament\Forms\Components\FileUpload as FilamentFileUpload;
use Livewire\Features\SupportFileUploads\TemporaryUploadedFile;

class FileUpload extends FilamentFileUpload
{
public function removeUploadedFile(string $fileKey): string | TemporaryUploadedFile | null
{
$result = parent::removeUploadedFile($fileKey);
$this->callAfterStateUpdated();
return $result;
}

public function reorderUploadedFiles(array $fileKeys): void
{
parent::reorderUploadedFiles($fileKeys);
$this->callAfterStateUpdated();
}
}
<?php

namespace App\Filament\Forms\Components;

use Filament\Forms\Components\FileUpload as FilamentFileUpload;
use Livewire\Features\SupportFileUploads\TemporaryUploadedFile;

class FileUpload extends FilamentFileUpload
{
public function removeUploadedFile(string $fileKey): string | TemporaryUploadedFile | null
{
$result = parent::removeUploadedFile($fileKey);
$this->callAfterStateUpdated();
return $result;
}

public function reorderUploadedFiles(array $fileKeys): void
{
parent::reorderUploadedFiles($fileKeys);
$this->callAfterStateUpdated();
}
}
Thanks!

Did you find this page helpful?