Skip to content

Commit 226e764

Browse files
committed
feat: fix loading images
1 parent 57db98d commit 226e764

File tree

2 files changed

+61
-28
lines changed

2 files changed

+61
-28
lines changed

src/Concerns/CanMapDynamicFields.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,10 @@ protected function getFieldsFromBlocks(array $blocks): Collection
318318

319319
collect($blocks)->map(function ($block) use (&$processedFields) {
320320
foreach ($block as $key => $values) {
321+
if (! is_array($values) || ! isset($values['data'])) {
322+
continue;
323+
}
324+
321325
$fields = $values['data'];
322326
$fields = ModelsField::whereIn('ulid', array_keys($fields))->get();
323327

src/Fields/FileUpload.php

Lines changed: 57 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Backstage\Fields\Contracts\FieldContract;
66
use Backstage\Fields\Models\Field;
7+
use Filament\Forms\Components\FileUpload as FilamentFileUpload;
78
use Filament\Forms\Components\Select;
89
use Filament\Forms\Components\TextInput;
910
use Filament\Forms\Components\Toggle;
@@ -35,36 +36,33 @@ public static function getDefaultConfig(): array
3536
];
3637
}
3738

38-
public static function make(string $name, ?Field $field = null): \Filament\Forms\Components\FileUpload
39+
public static function make(string $name, ?Field $field = null): FilamentFileUpload
3940
{
40-
$config = $field?->config ?? self::getDefaultConfig();
41+
$config = array_merge(self::getDefaultConfig(), $field?->config ?? []);
4142

42-
$component = \Filament\Forms\Components\FileUpload::make($name)
43+
$component = FilamentFileUpload::make($name)
4344
->label($field->name ?? null)
44-
->disk($config['disk'] ?? 'public')
45-
->directory($config['directory'] ?? 'uploads')
46-
->visibility($config['visibility'] ?? 'public')
47-
->maxFiles($config['maxFiles'] ?? 1)
48-
->multiple($config['multiple'] ?? false)
49-
->appendFiles($config['appendFiles'] ?? false)
50-
->reorderable($config['reorderable'] ?? false)
51-
->openable($config['openable'] ?? true)
52-
->downloadable($config['downloadable'] ?? true)
53-
->previewable($config['previewable'] ?? true)
54-
->deletable($config['deletable'] ?? true);
55-
56-
if (isset($config['acceptedFileTypes']) && $config['acceptedFileTypes']) {
45+
->disk($config['disk'])
46+
->directory($config['directory'])
47+
->visibility($config['visibility'])
48+
->maxFiles($config['maxFiles'])
49+
->multiple($config['multiple'])
50+
->appendFiles($config['appendFiles'])
51+
->reorderable($config['reorderable'])
52+
->openable($config['openable'])
53+
->downloadable($config['downloadable'])
54+
->previewable($config['previewable'])
55+
->deletable($config['deletable']);
56+
57+
if ($config['acceptedFileTypes']) {
5758
$component->acceptedFileTypes(explode(',', $config['acceptedFileTypes']));
5859
}
5960

60-
if (isset($config['maxSize']) && $config['maxSize']) {
61+
if ($config['maxSize']) {
6162
$component->maxSize($config['maxSize']);
6263
}
6364

64-
// Apply default settings
65-
$component = self::applyDefaultSettings($component, $field);
66-
67-
return $component;
65+
return self::applyDefaultSettings($component, $field);
6866
}
6967

7068
public static function mutateFormDataCallback(Model $record, Field $field, array $data): array
@@ -73,8 +71,7 @@ public static function mutateFormDataCallback(Model $record, Field $field, array
7371
return $data;
7472
}
7573

76-
$value = $record->values[$field->ulid];
77-
$data[$record->valueColumn][$field->ulid] = self::normalizeFileValue($value, $field);
74+
$data[$record->valueColumn][$field->ulid] = self::decodeFileValueForForm($record->values[$field->ulid]);
7875

7976
return $data;
8077
}
@@ -85,15 +82,44 @@ public static function mutateBeforeSaveCallback(Model $record, Field $field, arr
8582
return $data;
8683
}
8784

88-
$value = $data[$record->valueColumn][$field->ulid];
89-
$data[$record->valueColumn][$field->ulid] = self::normalizeFileValue($value, $field);
85+
$data[$record->valueColumn][$field->ulid] = self::normalizeFileValue($data[$record->valueColumn][$field->ulid]);
9086

9187
return $data;
9288
}
9389

94-
private static function normalizeFileValue($value, Field $field): mixed
90+
private static function decodeFileValueForForm(mixed $value): array
9591
{
96-
// Handle file upload values - they should be stored as JSON strings or arrays
92+
if (is_null($value) || $value === '') {
93+
return [];
94+
}
95+
96+
if (is_array($value)) {
97+
return $value;
98+
}
99+
100+
if (is_string($value) && json_validate($value)) {
101+
$decoded = json_decode($value, true);
102+
103+
return is_array($decoded) ? $decoded : [];
104+
}
105+
106+
if (is_string($value) && ! empty($value)) {
107+
return [$value];
108+
}
109+
110+
if (! empty($value)) {
111+
return [(string) $value];
112+
}
113+
114+
return [];
115+
}
116+
117+
private static function normalizeFileValue(mixed $value): ?string
118+
{
119+
if (is_null($value) || $value === '') {
120+
return null;
121+
}
122+
97123
if (is_array($value)) {
98124
return json_encode($value);
99125
}
@@ -102,11 +128,14 @@ private static function normalizeFileValue($value, Field $field): mixed
102128
return $value;
103129
}
104130

105-
// For single file uploads, convert to array format
106131
if (is_string($value) && ! empty($value)) {
107132
return json_encode([$value]);
108133
}
109134

135+
if (! empty($value)) {
136+
return json_encode([(string) $value]);
137+
}
138+
110139
return null;
111140
}
112141

0 commit comments

Comments
 (0)