Skip to content

Commit

Permalink
Add linkPickerParameterQuery to customize the parameter query (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
jyrkidn authored Nov 4, 2024
1 parent 23a838e commit be1ca1d
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
22 changes: 22 additions & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -227,3 +227,25 @@ This package comes with a helper function called `lroute()`, with it you can rea
```

If you have enabled the route to open in a new tab, the helper will automatically add the `target="_blank"` attribute to the link as well.

## Customizing the route parameters list

### Title field

We will show the `id` in the linkpicker dropdown when choosing a model for a parameter.
But this can be customized in your model, via the static property `$linkPickerTitleField`

```php
public static $linkPickerTitleField = 'working_title';
```

### Customizing parameter query

We will show all models in the parameter select, but this can be customized by adding a static method `linkPickerParameterQuery` on the model.

```php
public static function linkPickerParameterQuery($query): void
{
$query->where('is_published', true);
}
```
11 changes: 7 additions & 4 deletions src/Filament/LinkPickerInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,10 +190,13 @@ private function getFormSchemaForRoute(?string $selectedRoute): Collection
->label(Str::title($parameter->name))
->required(! $parameter->allowsNull())
->searchable()
->options($model::withoutGlobalScopes()->pluck(
$model::$linkPickerTitleField ?? 'id',
(new $model)->getKeyName(),
));
->options($model::query()
->when(method_exists($model, 'linkPickerParameterQuery'), fn ($query) => $model::linkPickerParameterQuery($query))
->pluck(
$model::$linkPickerTitleField ?? 'id',
(new $model)->getKeyName(),
)
);
});
}

Expand Down

0 comments on commit be1ca1d

Please sign in to comment.