Skip to content

Commit a6cce0e

Browse files
committed
fix: mutate form data before fill
1 parent 97193b2 commit a6cce0e

1 file changed

Lines changed: 16 additions & 8 deletions

File tree

src/Uploadcare.php

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public static function getDefaultConfig(): array
2828
public static function make(string $name, Field $field): Input
2929
{
3030
$input = self::applyDefaultSettings(
31-
input: Input::make($name)->removeCopyright(),
31+
input: Input::make($name)->withMetadata()->removeCopyright(),
3232
field: $field
3333
);
3434

@@ -84,6 +84,7 @@ public function getForm(): array
8484
];
8585
}
8686

87+
8788
public static function mutateFormDataCallback(Model $record, Field $field, array $data): array
8889
{
8990
if (! property_exists($record, 'valueColumn') || ! isset($record->values[$field->ulid])) {
@@ -93,13 +94,17 @@ public static function mutateFormDataCallback(Model $record, Field $field, array
9394
$values = $record->values[$field->ulid];
9495

9596
if ($values == '' || $values == [] || $values == null || empty($values)) {
96-
return [];
97+
$data[$record->valueColumn][$field->ulid] = [];
98+
99+
return $data;
97100
}
98101

99102
if ($field->config['withMetadata'] ?? self::getDefaultConfig()['withMetadata']) {
100103
$values = self::parseValues($values);
101104

102-
return $values;
105+
$data[$record->valueColumn][$field->ulid] = $values;
106+
107+
return $data;
103108
}
104109

105110
$values = self::parseValues($values);
@@ -110,14 +115,14 @@ public static function mutateFormDataCallback(Model $record, Field $field, array
110115
$mediaUrls = self::extractCdnUrlsFromFileData($values);
111116
}
112117

113-
$values = self::filterValidUrls($mediaUrls);
118+
$data[$record->valueColumn][$field->ulid] = self::filterValidUrls($mediaUrls);
114119

115-
return $values;
120+
return $data;
116121
}
117122

118123
public static function mutateBeforeSaveCallback(Model $record, Field $field, array $data): array
119124
{
120-
if (! property_exists($field, 'field_type') && $field->field_type !== 'uploadcare') {
125+
if (! property_exists($field, 'field_type') || $field->field_type !== 'uploadcare') {
121126
return $data;
122127
}
123128

@@ -129,13 +134,16 @@ public static function mutateBeforeSaveCallback(Model $record, Field $field, arr
129134

130135
if ($values === '' || $values === [] || $values === null) {
131136
$data[$record->valueColumn][$field->ulid] = null;
132-
133137
return $data;
134138
}
135139

136140
$values = self::normalizeValues($values);
137141

138-
$media = self::processUploadedFiles(is_array($values) ? $values : [$values]);
142+
if (! is_array($values)) {
143+
return $data;
144+
}
145+
146+
$media = self::processUploadedFiles($values);
139147
$data[$record->valueColumn][$field->ulid] = collect($media)->pluck('ulid')->toArray();
140148

141149
return $data;

0 commit comments

Comments
 (0)