|
7 | 7 | use Closure; |
8 | 8 | use Cone\Root\Filters\Filter; |
9 | 9 | use Cone\Root\Filters\RenderableFilter; |
| 10 | +use Cone\Root\Support\Copyable; |
10 | 11 | use Cone\Root\Traits\Authorizable; |
11 | 12 | use Cone\Root\Traits\HasAttributes; |
12 | 13 | use Cone\Root\Traits\Makeable; |
@@ -144,6 +145,11 @@ abstract class Field implements Arrayable, JsonSerializable |
144 | 145 | */ |
145 | 146 | protected bool|Closure $translatable = false; |
146 | 147 |
|
| 148 | + /** |
| 149 | + * Indicates whether the field is copyable. |
| 150 | + */ |
| 151 | + protected bool|Closure $copyable = false; |
| 152 | + |
147 | 153 | /** |
148 | 154 | * Create a new field instance. |
149 | 155 | */ |
@@ -360,6 +366,24 @@ public function resolveSearchQuery(Request $request, Builder $query, mixed $valu |
360 | 366 | : $query; |
361 | 367 | } |
362 | 368 |
|
| 369 | + /** |
| 370 | + * Set the copyable attribute. |
| 371 | + */ |
| 372 | + public function copyable(bool|Closure $value = true): static |
| 373 | + { |
| 374 | + $this->copyable = $value; |
| 375 | + |
| 376 | + return $this; |
| 377 | + } |
| 378 | + |
| 379 | + /** |
| 380 | + * Determine whether the field value is copyable. |
| 381 | + */ |
| 382 | + public function isCopyable(): bool |
| 383 | + { |
| 384 | + return $this->copyable instanceof Closure ? call_user_func($this->copyable) : $this->copyable; |
| 385 | + } |
| 386 | + |
363 | 387 | /** |
364 | 388 | * Set the translatable attribute. |
365 | 389 | */ |
@@ -520,15 +544,22 @@ public function resolveFormat(Request $request, Model $model): ?string |
520 | 544 | { |
521 | 545 | $value = $this->resolveValue($request, $model); |
522 | 546 |
|
523 | | - if (is_null($this->formatResolver)) { |
524 | | - return match (true) { |
525 | | - is_array($value) => json_encode($value), |
526 | | - is_null($value) => $value, |
527 | | - default => (string) $value, |
528 | | - }; |
| 547 | + $formattedValue = match (true) { |
| 548 | + ! is_null($this->formatResolver) => call_user_func_array($this->formatResolver, [$request, $model, $value]), |
| 549 | + is_array($value) => json_encode($value), |
| 550 | + is_null($value) => $value, |
| 551 | + default => (string) $value, |
| 552 | + }; |
| 553 | + |
| 554 | + if (! $this->isCopyable()) { |
| 555 | + return $formattedValue; |
529 | 556 | } |
530 | 557 |
|
531 | | - return call_user_func_array($this->formatResolver, [$request, $model, $value]); |
| 558 | + return (string) Copyable::make($formattedValue, match (true) { |
| 559 | + is_array($value) => json_encode($value), |
| 560 | + is_null($value) => $value, |
| 561 | + default => (string) $value, |
| 562 | + }); |
532 | 563 | } |
533 | 564 |
|
534 | 565 | /** |
|
0 commit comments