Skip to content

Commit

Permalink
[5.x] Add cache for nested imported fieldsets (#10280)
Browse files Browse the repository at this point in the history
  • Loading branch information
riasvdv authored Jun 12, 2024
1 parent 2952558 commit dacede2
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 28 deletions.
6 changes: 5 additions & 1 deletion src/Fields/Fields.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,11 @@ public function get($field)

public function toPublishArray()
{
return $this->fields->values()->map->toPublishArray()->all();
$blink = md5(json_encode($this->fields));

return Blink::once($blink, function () {
return $this->fields->values()->map->toPublishArray()->all();
});
}

public function addValues(array $values)
Expand Down
70 changes: 43 additions & 27 deletions src/Fieldtypes/Replicator.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Statamic\Fieldtypes;

use Facades\Statamic\Fieldtypes\RowId;
use Statamic\Facades\Blink;
use Statamic\Facades\GraphQL;
use Statamic\Fields\Fields;
use Statamic\Fields\Fieldtype;
Expand Down Expand Up @@ -118,12 +119,17 @@ protected function preProcessRow($row, $index)

public function fields($set, $index = -1)
{
return new Fields(
Arr::get($this->flattenedSetsConfig(), "$set.fields"),
$this->field()->parent(),
$this->field(),
$index
);
$config = Arr::get($this->flattenedSetsConfig(), "$set.fields");
$hash = md5($index.json_encode($config));

return Blink::once($hash, function () use ($config, $index) {
return new Fields(
$config,
$this->field()->parent(),
$this->field(),
$index
);
});
}

public function extraRules(): array
Expand Down Expand Up @@ -207,15 +213,21 @@ public function preload()
return [$set['_id'] => $this->fields($set['type'], $index)->addValues($set)->meta()->put('_', '_')];
})->toArray();

$defaults = collect($this->flattenedSetsConfig())->map(function ($set, $handle) {
return $this->fields($handle)->all()->map(function ($field) {
return $field->fieldtype()->preProcess($field->defaultValue());
$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();
})->all();
});

$new = collect($this->flattenedSetsConfig())->map(function ($set, $handle) use ($defaults) {
return $this->fields($handle)->addValues($defaults[$handle])->meta()->put('_', '_');
})->toArray();
$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();
});

$previews = collect($existing)->map(function ($fields) {
return collect($fields)->map(function () {
Expand All @@ -234,21 +246,25 @@ public function preload()

public function flattenedSetsConfig()
{
$sets = collect($this->config('sets'));

// If the first set doesn't have a nested "set" key, it would be the legacy format.
// We'll put it in a "main" group so it's compatible with the new format.
// This also happens in the "sets" fieldtype.
if (! Arr::has($sets->first(), 'sets')) {
$sets = collect([
'main' => [
'sets' => $sets->all(),
],
]);
}
$blink = md5($this->field?->handle().json_encode($this->field?->config()));

return Blink::once($blink, function () {
$sets = collect($this->config('sets'));

// If the first set doesn't have a nested "set" key, it would be the legacy format.
// We'll put it in a "main" group so it's compatible with the new format.
// This also happens in the "sets" fieldtype.
if (! Arr::has($sets->first(), 'sets')) {
$sets = collect([
'main' => [
'sets' => $sets->all(),
],
]);
}

return $sets->flatMap(function ($section) {
return $section['sets'];
return $sets->flatMap(function ($section) {
return $section['sets'];
});
});
}

Expand Down

0 comments on commit dacede2

Please sign in to comment.