From 9b20a067b1bc3f88eaa78b362db42e92e0b9e5d6 Mon Sep 17 00:00:00 2001 From: Duncan McClean Date: Mon, 16 Feb 2026 10:59:02 +0000 Subject: [PATCH] Preload replicator meta/defaults when blueprint doen't have a FQH --- .../fieldtypes/bard/BardFieldtype.vue | 8 +++++ .../fieldtypes/replicator/Replicator.vue | 8 +++++ src/Fieldtypes/Bard.php | 25 ++++++++++++++ src/Fieldtypes/Replicator.php | 33 +++++++++++++++++++ 4 files changed, 74 insertions(+) diff --git a/resources/js/components/fieldtypes/bard/BardFieldtype.vue b/resources/js/components/fieldtypes/bard/BardFieldtype.vue index 531ace671db..2dee36cdb83 100644 --- a/resources/js/components/fieldtypes/bard/BardFieldtype.vue +++ b/resources/js/components/fieldtypes/bard/BardFieldtype.vue @@ -527,6 +527,14 @@ export default { const reference = this.publishContainer.reference; const blueprint = this.publishContainer.blueprint.fqh; + if (this.meta.new?.hasOwnProperty(set)) { + let meta = this.meta.new[set]; + let defaults = this.meta.defaults[set]; + + resolve({ new: meta, defaults }); + return; + } + if (this.setsCache[setCacheKey]) { resolve(this.setsCache[setCacheKey]); return; diff --git a/resources/js/components/fieldtypes/replicator/Replicator.vue b/resources/js/components/fieldtypes/replicator/Replicator.vue index 11284642900..5ab4dba72d4 100644 --- a/resources/js/components/fieldtypes/replicator/Replicator.vue +++ b/resources/js/components/fieldtypes/replicator/Replicator.vue @@ -241,6 +241,14 @@ export default { const reference = this.publishContainer.reference; const blueprint = this.publishContainer.blueprint.fqh; + if (this.meta.new?.hasOwnProperty(set)) { + let meta = this.meta.new[set]; + let defaults = this.meta.defaults[set]; + + resolve({ new: meta, defaults }); + return; + } + if (this.setsCache[setCacheKey]) { resolve(this.setsCache[setCacheKey]); return; diff --git a/src/Fieldtypes/Bard.php b/src/Fieldtypes/Bard.php index e3568eefd51..3f9b8ce25ef 100644 --- a/src/Fieldtypes/Bard.php +++ b/src/Fieldtypes/Bard.php @@ -617,6 +617,18 @@ public function preload() return [$set['attrs']['id'] => $this->fields($values['type'], $index)->addValues($values)->meta()->put('_', '_')]; })->toArray(); + if ($this->shouldProcessNewValues()) { + $defaults = collect($this->flattenedSetsConfig())->map(function ($set, $handle) { + return $this->fields($handle)->all()->map(function ($field) { + return $field->fieldtype()->preProcess($field->defaultValue()); + })->all(); + })->all(); + + $new = collect($this->flattenedSetsConfig())->map(function ($set, $handle) use ($defaults) { + return $this->fields($handle)->addValues($defaults[$handle])->meta()->put('_', '_'); + })->toArray(); + } + $previews = collect($existing)->map(function ($fields) { return collect($fields)->map(function () { return null; @@ -637,6 +649,8 @@ public function preload() $data = [ 'existing' => $existing, + 'new' => $new ?? null, + 'defaults' => $defaults ?? null, 'collapsed' => $this->config('collapse') ? array_keys($existing) : [], 'previews' => $previews, '__collaboration' => ['existing'], @@ -663,6 +677,17 @@ public function preload() return $this->runHooks('preload', $data); } + private function shouldProcessNewValues(): bool + { + $parent = $this->field()->parent(); + + if (! $parent || ! method_exists($parent, 'blueprint')) { + return true; + } + + return is_null($parent->blueprint()->fullyQualifiedHandle()); + } + public function preProcessValidatable($value) { if (is_array($value)) { diff --git a/src/Fieldtypes/Replicator.php b/src/Fieldtypes/Replicator.php index 04552676ad9..2455095d3fa 100644 --- a/src/Fieldtypes/Replicator.php +++ b/src/Fieldtypes/Replicator.php @@ -228,12 +228,45 @@ public function preload() return [$set['_id'] => $this->fields($set['type'], $index)->addValues($set)->meta()->put('_', '_')]; })->toArray(); + // Most of the time, these values will be fetched over AJAX. + // However, if the blueprint doesn't have a FQH, we need to fallback here. + if ($this->shouldProcessNewValues()) { + $blink = md5(json_encode($this->flattenedSetsConfig())); + + $defaults = Blink::once($blink.'-defaults', function () { + return collect($this->flattenedSetsConfig())->map(function ($set, $handle) { + return $this->fields($handle)->all()->map(function ($field) { + return $field->fieldtype()->preProcess($field->defaultValue()); + })->all(); + })->all(); + }); + + $new = Blink::once($blink.'-new', function () use ($defaults) { + return collect($this->flattenedSetsConfig())->map(function ($set, $handle) use ($defaults) { + return $this->fields($handle)->addValues($defaults[$handle])->meta()->put('_', '_'); + })->toArray(); + }); + } + return [ 'existing' => $existing, + 'new' => $new ?? null, + 'defaults' => $defaults ?? null, 'collapsed' => $this->config('collapse') ? array_keys($existing) : [], ]; } + private function shouldProcessNewValues(): bool + { + $parent = $this->field()->parent(); + + if (! $parent || ! method_exists($parent, 'blueprint')) { + return true; + } + + return is_null($parent->blueprint()->fullyQualifiedHandle()); + } + public function flattenedSetsConfig() { $blink = md5($this->field?->handle().json_encode($this->field?->config()));