Filament Plugins

Purchase

Custom driver

Finally, one of the power-features of the Media Library is that is runs exclusively on drivers. This means that you can swap out the default driver and provide your own implementation that works with your existing data. Whilst still benefiting from all the features and entire UI of the Media Library.

Pros

  • Support literally anything you can think of. Unlimited flexibility.
  • Recommended for projects or legacy environments with a lot of data for which it is not feasible or practical to write an import script.

Cons

  • Some custom development work (not hard).

Installation

1

Create driver

To use a custom driver, you need to create a new PHP class in your projects that extends the abstract RalphJSmit\Filament\Explore\Drivers\Driver. Then, implement all methods that are provided by the Driver class:

app/MediaLibrary/CustomMediaLibraryDriver.php

use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Collection;
use RalphJSmit\Filament\Explore\Data\FileData;
use RalphJSmit\Filament\Explore\Data\TemporaryFileUploadData;
use RalphJSmit\Filament\Explore\Drivers\Driver;
use RalphJSmit\Filament\Explore\Enums\FileType;

class CustomMediaLibraryDriver extends Driver
{
    /**
     * If `true`, then adding a directory separator to a folder name does not move the folder.
     * If `false`, then adding or removing a directory separator to a folder name will move
     * the folder and result in a folder name as after the last directory separator.
     */
    public function allowsDirectorySeparatorInFolderName(): bool
    {
        //
    }

    public function createFile(?FileData $folder, TemporaryFileUploadData $temporaryFileUploadData): FileData
    {
        //
    }

    public function createFolder(?FileData $folder, string $name): FileData
    {
        //
    }

    public function deleteFile(FileData $file): void
    {
        //
    }

    public function findFile(?FileType $fileType, string $key, array $modifications = []): ?FileData
    {
        //
    }

    /**
     * @return Collection<FileData>
     */
    public function findFiles(?FileType $fileType, array $keys, array $modifications = []): Collection
    {
        //
    }

    public function getDirectorySeparator(): string
    {
        //
    }

    /**
     * @return Collection<FileData>
     */
    public function getFiles(?FileData $folder = null, array $modifications = []): Collection
    {
        //
    }

    public function getFilesCount(?FileData $folder = null, array $modifications = []): int
    {
        //
    }

    public function getFilesPaginator(?FileData $folder = null, array $modifications = [], int $page = 1, int $perPage = 50): LengthAwarePaginator
    {
        //
    }

    public function getMaxFileSizeKb(): int
    {
        //
    }

    public function moveFile(FileData $file, ?FileData $to): void
    {
        //
    }
}

The custom driver only needs to implement methods that are not already provided by the abstract Driver class.

You are totally free to implement all these methods. This could be integrating with a remote API, another server, local disk or a set of internal APIs.

You probably also need to implement a custom SearchProvider to allow searching through the library. You are free to add public methods and properties to your driver class to make it configurable from the outside:

If in the future new features and methods are added to the Driver class, attention will be given to provide a default implementation that does not break existing drivers or to make a new feature opt-in and provide an empty method.

2

Register driver

Next, we need to register the driver you created:

app/Providers/Filament/YourPanelProvider.php

$plugin
    ->driver(CustomMediaLibraryDriver::class)
3

Dynamic configuration

If you need to configure your driver dynamically, you are free to add any public methods to your driver class, and configure them dynamically as follows:

app/Providers/Filament/YourPanelProvider.php

$plugin
    ->driver(CustomMediaLibraryDriver::class, function (CustomMediaLibraryDriver $driver) {
        // ...
    })

Now your library will run on the custom driver you created! Any already existing files should show up in the library right away.

© FilamentPlugins.com ✦ 2022 – 2025 ✦ All rights reserved.