From d782623accc29403ef37a7b2beba014ca00202d7 Mon Sep 17 00:00:00 2001 From: Juris <78373300+jbumaniss@users.noreply.github.com> Date: Fri, 9 Feb 2024 16:44:04 +0200 Subject: [PATCH] Enhance Link Creation with Filtered Resource Index Support (#36) Co-authored-by: juris.bumanis Co-authored-by: Rafael Milewski --- src/Link.php | 30 ++++++++++++++++++++++++++++++ src/NovaResource.php | 2 +- 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/src/Link.php b/src/Link.php index 43d4790..392dc54 100644 --- a/src/Link.php +++ b/src/Link.php @@ -8,6 +8,8 @@ use Closure; use JsonSerializable; use Laravel\Nova\AuthorizedToSee; +use Laravel\Nova\Filters\Filter; +use Laravel\Nova\Filters\FilterEncoder; use Laravel\Nova\Lenses\Lens; use Laravel\Nova\Makeable; @@ -24,6 +26,8 @@ class Link implements JsonSerializable protected string $url; + private array $filters = []; + public function __construct( private readonly string $label, ) @@ -97,8 +101,34 @@ public function openInNewTab(bool|Closure $openInNewTab = true): self return $this; } + /** + * @param class-string|Filter $filter + */ + public function addFilter(string|Filter $filter, mixed $value): self + { + $filter = is_string($filter) ? resolve($filter) : $filter; + + $this->filters[] = [ $filter->key() => $value ]; + + return $this; + } + public function jsonSerialize(): array { + if (filled($this->filters)) { + + $filters = new FilterEncoder($this->filters); + + if ($this instanceof NovaResource) { + + $this->url( + sprintf('%s?%s_filter=%s', $this->url, $this->resourceUriKey, $filters->encode()), + ); + + } + + } + return [ 'label' => $this->label, 'url' => $this->url, diff --git a/src/NovaResource.php b/src/NovaResource.php index 761920a..0129fb1 100644 --- a/src/NovaResource.php +++ b/src/NovaResource.php @@ -11,7 +11,7 @@ class NovaResource extends Link { - private string $resourceUriKey; + protected string $resourceUriKey; /** * @param class-string $resource