Components
Media Library
Integrations
Sorters
You can use sorters in the media library to sort the returned selection of media items.
Default sorters
By default, the Media Library comes with the following sorters:
Name
The name sorter allows you to order media items by name.
Updated
The updated sorter allows you to order media items by updated date.
Size
The size sorter allows you to sort media items by mime type.
Type
The type sorter allows you to sort media items by type.
Custom sorter
Creating a custom sorter requires several steps to link it up correctly with the driver. In this example, we'll create a sorter that allows users to sort media items based on whether they are marked as "featured".
Create sorter
First, the sorter-class must be created. This class is responsible for rendering the sorter and defining which modifications should be applied when the sorter is used.
Register sorter
Next, the sorter must be registered on the library:
Create modification
Next, a modification needs to be created. A modification is returned from the sorter, and is responsible for holding the "information" about what needs to be ordered:
Generally, order-modifications only contain the
sortDirection()methods.
Apply modification
Finally, it must be told to the driver how to exactly apply the above modification:
If you have not yet made the choice for a driver, consider using the default recommended Media Library Item-driver. As this driver is database-based, you can easily add custom columns to the
filament_media_library-table where you can conveniently store such extra meta-data as whether an item is featured.
This way, the sorter will determine what modifications need to be applied when retrieving the media results, and the driver knows how to interpret these modifications and will apply them.
In the UI, the sorter will now show up as such:
You can read more about modifications and their types in the Modifications documentation.