dev-think-one /
nova-entity-select-field
| 1 | <?php |
||
| 2 | |||
| 3 | namespace NovaEntitySelectField; |
||
| 4 | |||
| 5 | use Laravel\Nova\Fields\Field; |
||
| 6 | use Laravel\Nova\Fields\Searchable; |
||
| 7 | use Laravel\Nova\Http\Requests\NovaRequest; |
||
| 8 | use Laravel\Nova\Nova; |
||
| 9 | use Laravel\Nova\Resource; |
||
| 10 | |||
| 11 | /** |
||
| 12 | * @psalm-suppress MethodSignatureMismatch |
||
| 13 | * @method static static make(string $entityResource, ?string $name = null, ?string $attribute = null, callable|null $resolveCallback = null) |
||
| 14 | */ |
||
| 15 | class EntitySelect extends Field |
||
| 16 | { |
||
| 17 | use Searchable; |
||
| 18 | |||
| 19 | public $component = 'entity-select-field'; |
||
| 20 | |||
| 21 | /** @var class-string<Resource> */ |
||
|
0 ignored issues
–
show
Documentation
Bug
introduced
by
Loading history...
|
|||
| 22 | public string $entityResource; |
||
| 23 | |||
| 24 | public int $limit = 10; |
||
| 25 | |||
| 26 | public ?string $displayValue = null; |
||
| 27 | |||
| 28 | public ?bool $viewable = null; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @param class-string<Resource> $entityResource |
||
|
0 ignored issues
–
show
|
|||
| 32 | * @param $name |
||
| 33 | * @param $attribute |
||
| 34 | * @param callable|null $resolveCallback |
||
| 35 | */ |
||
| 36 | 3 | public function __construct(string $entityResource, $name = null, $attribute = null, callable $resolveCallback = null) |
|
| 37 | { |
||
| 38 | 3 | $this->entityResource = $entityResource; |
|
| 39 | |||
| 40 | 3 | if (!$name) { |
|
| 41 | 3 | $name = Nova::humanize(class_basename($entityResource)); |
|
| 42 | } |
||
| 43 | |||
| 44 | 3 | parent::__construct($name, $attribute, $resolveCallback); |
|
| 45 | } |
||
| 46 | |||
| 47 | 1 | public function limit($limit) |
|
| 48 | { |
||
| 49 | 1 | $this->limit = $limit; |
|
| 50 | |||
| 51 | 1 | return $this; |
|
| 52 | } |
||
| 53 | |||
| 54 | 1 | public function viewable($value = true) |
|
| 55 | { |
||
| 56 | 1 | $this->viewable = $value; |
|
| 57 | |||
| 58 | 1 | return $this; |
|
| 59 | } |
||
| 60 | |||
| 61 | 1 | public function resolveForDisplay($resource, $attribute = null) |
|
| 62 | { |
||
| 63 | 1 | parent::resolveForDisplay($resource, $attribute); |
|
| 64 | |||
| 65 | 1 | $titleColumn = $this->entityResource::$title; |
|
| 66 | |||
| 67 | 1 | $currentEntity = $this->entityResource::newModel()::query()->whereKey($this->value)->first(); |
|
| 68 | |||
| 69 | 1 | if ($currentEntity) { |
|
| 70 | 1 | $this->displayValue = $currentEntity->$titleColumn; |
|
| 71 | } |
||
| 72 | } |
||
| 73 | |||
| 74 | 2 | public function jsonSerialize(): array |
|
| 75 | { |
||
| 76 | 2 | $request = app(NovaRequest::class); |
|
| 77 | |||
| 78 | 2 | $viewable = !is_null($this->viewable) ? $this->viewable : $this->entityResource::authorizedToViewAny($request); |
|
| 79 | |||
| 80 | 2 | return array_merge(parent::jsonSerialize(), [ |
|
| 81 | 2 | 'entityResourceKey' => $this->entityResource::uriKey(), |
|
| 82 | 2 | 'softDeletes' => $this->entityResource::softDeletes(), |
|
| 83 | 2 | 'withSubtitles' => $this->withSubtitles, |
|
| 84 | 2 | 'debounce' => $this->debounce, |
|
| 85 | 2 | 'limit' => $this->limit, |
|
| 86 | 2 | 'viewable' => $viewable, |
|
| 87 | 2 | 'display' => $this->displayValue, |
|
| 88 | 2 | ]); |
|
| 89 | } |
||
| 90 | } |
||
| 91 |