Skip to content

Commit

Permalink
Enhance Link Creation with Filtered Resource Index Support (#36)
Browse files Browse the repository at this point in the history
Co-authored-by: juris.bumanis <[email protected]>
Co-authored-by: Rafael Milewski <[email protected]>
  • Loading branch information
3 people authored Feb 9, 2024
1 parent 4ef4b7c commit d782623
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
30 changes: 30 additions & 0 deletions src/Link.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -24,6 +26,8 @@ class Link implements JsonSerializable

protected string $url;

private array $filters = [];

public function __construct(
private readonly string $label,
)
Expand Down Expand Up @@ -97,8 +101,34 @@ public function openInNewTab(bool|Closure $openInNewTab = true): self
return $this;
}

/**
* @param class-string<Filter>|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,
Expand Down
2 changes: 1 addition & 1 deletion src/NovaResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

class NovaResource extends Link
{
private string $resourceUriKey;
protected string $resourceUriKey;

/**
* @param class-string<BaseNovaResource> $resource
Expand Down

0 comments on commit d782623

Please sign in to comment.