This package adds a field to pick a link from your defined routes or external links. It also adds a blade component to render the links.
The link picker field will show the following options:
The link picker will automatically show and bind the parameters of the selected route. This includes:
The link picker can also show route options:
is_download
to specify if the link is a download link.opens_in_new_tab
to specify if the link should open in a new tab.The link can be rendered using the <x-filament-link-picker-link />
blade component.
You can install the package via composer:
composer require outerweb/filament-link-picker
Add the plugin to your desired Filament panel:
use OuterWeb\FilamentLinkPicker\Filament\FilamentLinkPicker;Â class FilamentPanelProvider extends PanelProvider{ public function panel(Panel $panel): Panel { return $panel // ... ->plugins([ FilamentLinkPickerPlugin::make(), ]); }}
You can disable the external link option by adding the disableExternalLinks
method to the plugin.
FilamentLinkPickerPlugin::make() ->disableExternalLinks();
You can disable the email link option by adding the disableMailto
method to the plugin.
FilamentLinkPickerPlugin::make() ->disableMailto();
You can disable the telephone link option by adding the disableTel
method to the plugin.
FilamentLinkPickerPlugin::make() ->disableTel();
You can disable the 'download' option by adding the disableDownload
method to the plugin.
FilamentLinkPickerPlugin::make() ->disableDownload();
You can disable the 'opens in new tab' option by adding the disableOpenInNewTab
method to the plugin.
FilamentLinkPickerPlugin::make() ->disableOpenInNewTab();
You can enable multi language support by adding the translateLabels
method to the plugin.
FilamentLinkPickerPlugin::make() ->translateLabels();
The following items can be translated:
filamentLinkPicker()
method.filamentLinkPicker()
method.filamentLinkPicker()
method.To do so, just set the values to the translation key.
Route::get('/your-route/{parameter}', YourController::class) ->name('your-route') ->filamentLinkPicker( label: 'your-route.label', group: 'your-route.group', parameterLabels: [ 'parameter' => 'your-route.parameter', ] );
You can mark a route for the link picker to discover by adding the filamentLinkPicker()
method to the route.
use Illuminate\Support\Facades\Route;Â Route::get('/your-route', YourController::class) ->name('your-route') ->filamentLinkPicker();
The label of the route will be used in the dropdown of the link picker. You can customize the label by passing it as an argument to the filamentLinkPicker()
method.
Route::get('/your-route', YourController::class) ->name('your-route') ->filamentLinkPicker( label: 'Your custom label' );
By default, the label will be the route's name. If the route name contains dots, they will be replaced by '>'.
The group will be used to group the routes in the dropdown of the link picker. You can customize the group by passing it as an argument to the filamentLinkPicker()
method.
Route::get('/your-route', YourController::class) ->name('your-route') ->filamentLinkPicker( group: 'Your custom group' );
Marking a route as 'localized' will make the link picker combine all localized versions of that route. This is useful when you have a multi-language where you have the same route for different languages so that the link picker does not show the same route multiple times. See the Localization section for more information.
Route::get('/your-route', YourController::class) ->name('your-route') ->filamentLinkPicker( isLocalized: true );
You can define specific parameter labels for the route by passing an array to the filamentLinkPicker()
method.
These labels will be used in the link picker as labels for the parameter input fields.
Route::get('/your-route/{parameter}', YourController::class) ->name('your-route') ->filamentLinkPicker( parameterLabels: [ 'your-parameter' => 'Your parameter label' ] );
You can define specific parameter options for the route by passing an array to the filamentLinkPicker()
method.
These options will be used in the link picker as options for the parameter select field.
Route::get('/your-route/{parameter}', YourController::class) ->name('your-route') ->filamentLinkPicker( parameterOptions: [ 'your-parameter' => [ 'option1' => 'Option 1', 'option2' => 'Option 2', ] ] );
By default, the package will use the $model->getKey()
method to store the selected value of a parameter in the database. If you want to store the value in a different column, you can pass the column name as an argument to the filamentLinkPicker()
method.
Route::get('/event/{event:slug}', 'show')->name('show')->filamentLinkPicker( label: 'Single Event', group: 'Details Page', parameterModelKeys: [ 'event' => 'slug', ] );
Route model binding is supported by default. The link picker will automatically show the available models in the parameter select field. It will save the primary key of the model to the database.
By default, we go through the following attributes to automatically find the model's label:
If none of these attributes are found, the application will throw an exception.
You can explicitly define the model's label by adding a getLinkPickerLabel
method to the model.
class YourModel extends Model{ public function getLinkPickerLabel(): string { return $this->your_attribute; }}
Or you can define the label as a property on the model.
class YourModel extends Model{ public string $linkPickerLabel = 'your_attribute';}
You can filter the available models by adding a scopeLinkPickerOptions
method to the model.
This scope will then be applied to the model's query when fetching the available options for the select field.
class YourModel extends Model{ public function scopeLinkPickerOptions(Builder $query): void { // Your query here }}
The link picker can handle localized routes well. To do so, follow these steps:
localized
argument to the filamentLinkPicker()
method on your route.Route::get('/your-route', YourController::class) ->name('your-route') ->filamentLinkPicker( isLocalized: true );
combineLocalizedRoutesUsing
method to the plugin.use Outerweb\FilamentLinkPicker\Entities\LinkPickerRoute;Â FilamentLinkPickerPlugin::make() ->combineLocalizedRoutesUsing(function (LinkPickerRoute $route): LinkPickerRoute { // Imagine your routes are named like `en.your-route` and `nl.your-route` // We want to combine these routes so that the link picker only shows one option for `your-route` return $route->name(Str::after($route->name(), '.')); });
By default, the package will combine the localized routes by removing the part of the name before the first dot. If you want to use this behavior, you do not need to specify the combineLocalizedRoutesUsing
method.
buildLocalizedRouteUsing
method to the plugin.use Outerweb\FilamentLinkPicker\Entities\LinkPickerRoute;Â FilamentLinkPickerPlugin::make() ->buildLocalizedRouteUsing(function (string $name, array $parameters = [], bool $absolute = true, ?string $locale = null): ?string { // If you use our `outerweb/localization` package, // You can use the `localizedRoute` helper. return localizedRoute($name, $parameters, $absolute, $locale); });
As a fallback, when you do not specify a callback function, the link picker will use the outerweb/localization
package to build the localized routes if it is installed. This package provides a localizedRoute
helper that you can use in your application. Read more about this package here.
To use the actual value of the link, you can cast the attribute in your model or use the facade.
You can cast your model's attribute to the Outerweb\FilamentLinkPicker\Entities\Link
class to take advantage of the entity's properties and methods.
use Outerweb\FilamentLinkPicker\Casts\LinkCast;Â class YourModel extends Model{ protected $casts = [ 'link' => LinkCast::class, ];}
You can use the Outerweb\FilamentLinkPicker\Facades\LinkPicker
facade to cast the value to a Link
entity.
use Outerweb\FilamentLinkPicker\Facades\LinkPicker;Â $link = LinkPicker::dataToLinkEntity(array $data);
You can use the <x-filament-link-picker-link />
blade component to render the link.
<x-filament-link-picker-link :link="$link"> Your label here</x-filament-link-picker-link>
The link picker options like is_download
and opens_in_new_tab
will only take effect when you do not specify them on the component itself. If you do specify them, the component will use the specified values.
<x-filament-link-picker-link :link="$link" download target="_self" />
Here, we specified the download
and target
attributes. The component will use these values instead of the ones from the link picker.
Please see CHANGELOG for more information on what has changed recently.
The MIT License (MIT). Please see License File for more information.