Skip to content

Commit 76549ad

Browse files
author
Backstage
committed
Merge branch '2.x' of github.com:backstagephp/cms into 2.x
1 parent 67af7b0 commit 76549ad

File tree

2 files changed

+69
-2
lines changed

2 files changed

+69
-2
lines changed

src/Fields/Base.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,19 @@ public static function getFieldValueFromRecord(Model $record, Field $field): mix
202202

203203
// Handle relationship-based values (like Content model)
204204
if (self::isRelationship($values)) {
205-
$fieldValue = $values->where('field_ulid', $field->ulid)->first();
205+
$fieldValue = $values->where(function ($query) use ($field) {
206+
$query->where('field_ulid', $field->ulid)
207+
->orWhere('ulid', $field->ulid);
208+
})->first();
209+
210+
if ($field->slug === 'banner-image') {
211+
\Illuminate\Support\Facades\Log::info("[BASE DEBUG] getFieldValueFromRecord relation check", [
212+
'field_ulid' => $field->ulid,
213+
'record_key' => $record->getKey(),
214+
'found' => (bool) $fieldValue,
215+
'sql' => $values->where('field_ulid', $field->ulid)->toSql(),
216+
]);
217+
}
206218
$result = $fieldValue ? self::resolveHydratedValue($fieldValue) : null;
207219
}
208220
}

src/Fields/Repeater.php

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@
2525
use Illuminate\Support\Str;
2626
use 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

Comments
 (0)