Skip to content

Commit b6982b6

Browse files
authored
Refactor resolveTranslationsField method
Refactor resolveTranslationsField method for clarity and functionality.
1 parent 37eefdf commit b6982b6

File tree

1 file changed

+20
-11
lines changed

1 file changed

+20
-11
lines changed

src/Resources/Resource.php

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use Cone\Root\Filters\TrashStatus;
2323
use Cone\Root\Http\Middleware\Authorize;
2424
use Cone\Root\Interfaces\Form;
25+
use Cone\Root\Models\Translation;
2526
use Cone\Root\Root;
2627
use Cone\Root\Support\Alert;
2728
use Cone\Root\Traits\AsForm;
@@ -350,17 +351,25 @@ public function translatable(): bool
350351
*/
351352
public function resolveTranslationsField(Request $request): ?Translations
352353
{
353-
return $this->translatable()
354-
? Translations::make()
355-
->withFields(fn (): array => $this->resolveFields($request)
356-
->translatable()
357-
->map(static fn (Field $field): Field => (clone $field)
358-
->translatable(false)
359-
->setModelAttribute($key = 'values->'.$field->getModelAttribute())
360-
->name($key)
361-
->id($key))
362-
->all())
363-
: null;
354+
if (! $this->translatable()) {
355+
return null;
356+
}
357+
358+
return Translations::make()
359+
->withFields(function () use ($request): array {
360+
return $this->resolveFields($request)
361+
->translatable()
362+
->map(static function (Field $field): Field {
363+
return (clone $field)
364+
->translatable(false)
365+
->value(static function (Request $request, Translation $model) use ($field): mixed {
366+
return $model->values[$field->getModelAttribute()] ?? null;
367+
})
368+
->hydrate(static function (Request $request, Translation $model, mixed $value) use ($field): void {
369+
$model->values[$field->getModelAttribute()] = $value;
370+
});
371+
})->all();
372+
});
364373
}
365374

366375
/**

0 commit comments

Comments
 (0)