1010use Filament \Schemas \Components \Grid ;
1111use Filament \Schemas \Components \Tabs ;
1212use Filament \Schemas \Components \Tabs \Tab ;
13+ use Illuminate \Database \Eloquent \Model ;
14+ use Illuminate \Database \Eloquent \Relations \Relation ;
1315
1416class RichEditor extends Base implements FieldContract
1517{
@@ -60,7 +62,7 @@ private static function configureStateHandling(Input $input, string $name): Inpu
6062 });
6163 }
6264
63- private static function formatRichEditorState ($ state )
65+ private static function formatRichEditorState (mixed $ state ): mixed
6466 {
6567 if (empty ($ state )) {
6668 return null ;
@@ -113,22 +115,22 @@ private static function cleanContentArray(array $state): array
113115 return $ state ;
114116 }
115117
116- public static function mutateBeforeSaveCallback ($ record , $ field , array $ data ): array
118+ public static function mutateBeforeSaveCallback (Model $ record , Field $ field , array $ data ): array
117119 {
118120 $ data = self ::ensureRichEditorDataFormat ($ record , $ field , $ data );
119121
120122 return $ data ;
121123 }
122124
123- private static function ensureRichEditorDataFormat ($ record , $ field , array $ data ): array
125+ private static function ensureRichEditorDataFormat (Model $ record , Field $ field , array $ data ): array
124126 {
125127 $ data = self ::normalizeContentResourceValue ($ data , $ field );
126128 $ data = self ::normalizeDynamicFieldValue ($ record , $ data , $ field );
127129
128130 return $ data ;
129131 }
130132
131- private static function normalizeContentResourceValue (array $ data , $ field ): array
133+ private static function normalizeContentResourceValue (array $ data , Field $ field ): array
132134 {
133135 if (isset ($ data ['values ' ][$ field ->ulid ]) && empty ($ data ['values ' ][$ field ->ulid ])) {
134136 $ data ['values ' ][$ field ->ulid ] = '' ;
@@ -137,7 +139,7 @@ private static function normalizeContentResourceValue(array $data, $field): arra
137139 return $ data ;
138140 }
139141
140- private static function normalizeDynamicFieldValue ($ record , array $ data , $ field ): array
142+ private static function normalizeDynamicFieldValue (Model $ record , array $ data , Field $ field ): array
141143 {
142144 if (isset ($ data [$ record ->valueColumn ][$ field ->ulid ]) && empty ($ data [$ record ->valueColumn ][$ field ->ulid ])) {
143145 $ data [$ record ->valueColumn ][$ field ->ulid ] = '' ;
@@ -146,10 +148,9 @@ private static function normalizeDynamicFieldValue($record, array $data, $field)
146148 return $ data ;
147149 }
148150
149- public static function mutateFormDataCallback ($ record , $ field , array $ data ): array
151+ public static function mutateFormDataCallback (Model $ record , Field $ field , array $ data ): array
150152 {
151- // Get the raw value from the database without JSON decoding
152- $ rawValue = $ record ->values ()->where ('field_ulid ' , $ field ->ulid )->first ()?->value;
153+ $ rawValue = self ::getFieldValueFromRecord ($ record , $ field );
153154
154155 if ($ rawValue !== null ) {
155156 $ data [$ record ->valueColumn ][$ field ->ulid ] = $ rawValue ;
@@ -158,6 +159,36 @@ public static function mutateFormDataCallback($record, $field, array $data): arr
158159 return $ data ;
159160 }
160161
162+ private static function getFieldValueFromRecord (Model $ record , Field $ field ): mixed
163+ {
164+ // Check if record has values method
165+ if (!method_exists ($ record , 'values ' ) || !is_callable ([$ record , 'values ' ])) {
166+ return null ;
167+ }
168+
169+ $ values = $ record ->values ();
170+
171+ // Handle relationship-based values (like Content model)
172+ if (self ::isRelationship ($ values )) {
173+ return $ values ->where ('field_ulid ' , $ field ->ulid )->first ()?->value;
174+ }
175+
176+ // Handle array/collection-based values (like Settings model)
177+ if (is_array ($ values ) || $ values instanceof \Illuminate \Support \Collection) {
178+ return $ values [$ field ->ulid ] ?? null ;
179+ }
180+
181+ return null ;
182+ }
183+
184+ private static function isRelationship (mixed $ values ): bool
185+ {
186+ return is_object ($ values )
187+ && method_exists ($ values , 'where ' )
188+ && method_exists ($ values , 'get ' )
189+ && !($ values instanceof \Illuminate \Support \Collection);
190+ }
191+
161192 public function getForm (): array
162193 {
163194 return [
0 commit comments