2525use Illuminate \Support \Str ;
2626use Saade \FilamentAdjacencyList \Forms \Components \AdjacencyList ;
2727
28- class Repeater extends Base implements FieldContract
28+ use Backstage \Fields \Contracts \HydratesValues ;
29+ use Illuminate \Database \Eloquent \Model ;
30+
31+ class Repeater extends Base implements FieldContract, HydratesValues
2932{
3033 use HasConfigurableFields;
3134 use HasFieldTypeResolver;
@@ -36,6 +39,58 @@ public function getFieldType(): ?string
3639 return 'repeater ' ;
3740 }
3841
42+ public function hydrate (mixed $ value , ?Model $ model = null ): mixed
43+ {
44+ if (! is_array ($ value )) {
45+ return $ value ;
46+ }
47+
48+ if (empty ($ this ->field_model )) {
49+ file_put_contents ('/tmp/repeater_debug.log ' , "Field model missing for repeater. \n" , FILE_APPEND );
50+ return $ value ;
51+ }
52+
53+ $ children = $ this ->field_model ->children ->keyBy ('ulid ' );
54+ $ slugMap = $ this ->field_model ->children ->pluck ('ulid ' , 'slug ' );
55+
56+ file_put_contents ('/tmp/repeater_debug.log ' , "Hydrating Repeater " . $ this ->field_model ->ulid . " with children slugs: " . implode (', ' , $ slugMap ->keys ()->toArray ()) . "\n" , FILE_APPEND );
57+
58+ $ hydrated = [];
59+
60+ foreach ($ value as $ key => $ row ) {
61+ $ hydratedRow = $ row ;
62+
63+ if (is_array ($ row )) {
64+ foreach ($ row as $ fieldSlug => $ fieldValue ) {
65+ $ fieldUlid = $ slugMap [$ fieldSlug ] ?? null ;
66+ if ($ fieldUlid && isset ($ children [$ fieldUlid ])) {
67+ $ fieldModel = $ children [$ fieldUlid ];
68+ $ fieldClass = self ::resolveFieldTypeClassName ($ fieldModel ->field_type );
69+
70+ file_put_contents ('/tmp/repeater_debug.log ' , " > Hydrating field $ fieldSlug ( $ fieldModel ->field_type ) using $ fieldClass \n" , FILE_APPEND );
71+
72+ if ($ fieldClass && in_array (HydratesValues::class, class_implements ($ fieldClass ))) {
73+ // Instantiate the field class to access its hydrate method
74+ // We need to set the field model on the instance if possible,
75+ // or at least pass context if needed.
76+ // Assuming simpler 'make' or instantiation works for hydration context.
77+ $ fieldInstance = new $ fieldClass ();
78+ if (property_exists ($ fieldInstance , 'field_model ' )) {
79+ $ fieldInstance ->field_model = $ fieldModel ;
80+ }
81+
82+ $ hydratedRow [$ fieldSlug ] = $ fieldInstance ->hydrate ($ fieldValue , $ model );
83+ }
84+ }
85+ }
86+ }
87+
88+ $ hydrated [$ key ] = $ hydratedRow ;
89+ }
90+
91+ return $ hydrated ;
92+ }
93+
3994 public static function getDefaultConfig (): array
4095 {
4196 return [
0 commit comments