generated from filamentphp/plugin-skeleton
-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added ScoutSelect component, mofidied plugin structure
- Loading branch information
Showing
5 changed files
with
171 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
namespace Kainiklas\FilamentScout; | ||
|
||
use Filament\Contracts\Plugin; | ||
use Filament\Panel; | ||
use Kainiklas\FilamentScout\Providers\MeilisearchGlobalSearchProvider; | ||
use Kainiklas\FilamentScout\Providers\ScoutGlobalSearchProvider; | ||
use Kainiklas\FilamentScout\Traits\ConfigurePlugin; | ||
|
||
class FilamentScoutPlugin implements Plugin | ||
{ | ||
use ConfigurePlugin; | ||
|
||
public function getId(): string | ||
{ | ||
return 'kainiklas-filament-scout-plugin'; | ||
} | ||
|
||
public function register(Panel $panel): void | ||
{ | ||
// | ||
} | ||
|
||
public function boot(Panel $panel): void | ||
{ | ||
// Global Search Provider | ||
if ($this->getUseMeiliSearch()) { | ||
$panel->globalSearch(MeilisearchGlobalSearchProvider::class); | ||
} else { | ||
$panel->globalSearch(ScoutGlobalSearchProvider::class); | ||
} | ||
} | ||
|
||
public static function make(): static | ||
{ | ||
return app(static::class); | ||
} | ||
|
||
public static function get(): static | ||
{ | ||
/** @var static $plugin */ | ||
$plugin = filament(app(static::class)->getId()); | ||
|
||
return $plugin; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
namespace Kainiklas\FilamentScout\Forms\Components; | ||
|
||
use Closure; | ||
use Filament\Forms\Components\Select as FilamentSelect; | ||
use Illuminate\Database\Eloquent\Relations\Relation; | ||
use Illuminate\Support\Str; | ||
|
||
class ScoutSelect extends FilamentSelect | ||
{ | ||
public function relationship(string | Closure | null $name = null, string | Closure | null $titleAttribute = null, ?Closure $modifyQueryUsing = null): static | ||
{ | ||
parent::relationship($name, $titleAttribute, $modifyQueryUsing); | ||
|
||
$this->getSearchResultsUsing(function (ScoutSelect $component, string $search): array { | ||
$relationship = Relation::noConstraints(fn () => $component->getRelationship()); | ||
$qualifiedRelatedKeyName = $component->getQualifiedRelatedKeyNameForRelationship($relationship); | ||
|
||
return $relationship | ||
->getRelated() | ||
->search($search, function ($scout, string $query, array $options) { | ||
$options['limit'] = $this->getOptionsLimit(); | ||
|
||
return $scout->search($query, $options); | ||
}) | ||
->get() | ||
->pluck($component->getRelationshipTitleAttribute(), Str::afterLast($qualifiedRelatedKeyName, '.')) | ||
->toArray(); | ||
}); | ||
|
||
return $this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,6 @@ | |
use Filament\GlobalSearch\Contracts\GlobalSearchProvider; | ||
use Filament\GlobalSearch\GlobalSearchResult; | ||
use Filament\GlobalSearch\GlobalSearchResults; | ||
use Filament\Resources\Resource; | ||
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\Support\HtmlString; | ||
|
||
|
@@ -18,7 +17,7 @@ public function getResults(string $query): ?GlobalSearchResults | |
$builder = GlobalSearchResults::make(); | ||
|
||
foreach (Filament::getResources() as $resource) { | ||
/** @var resource $resource * */ | ||
/** @var Filament\Resources\Resource $resource * */ | ||
if (! $resource::canGloballySearch()) { | ||
Check failure on line 21 in src/Providers/MeilisearchGlobalSearchProvider.php GitHub Actions / phpstan
Check failure on line 21 in src/Providers/MeilisearchGlobalSearchProvider.php GitHub Actions / phpstan
|
||
continue; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
namespace Kainiklas\FilamentScout\Traits; | ||
|
||
trait ConfigurePlugin | ||
{ | ||
// use EvaluatesClosures; | ||
|
||
protected bool $useMeiliSearch = false; | ||
|
||
// protected bool $useScoutInSelect = false; | ||
|
||
public function useMeiliSearch(bool $condition = true): static | ||
{ | ||
$this->useMeiliSearch = $condition; | ||
|
||
return $this; | ||
} | ||
|
||
public function getUseMeiliSearch(): bool | ||
{ | ||
return $this->useMeiliSearch; | ||
} | ||
} |