Skip to content

Commit a9a0abb

Browse files
committed
Fix issue with imported fields and fieldsets
1 parent dde2c06 commit a9a0abb

File tree

1 file changed

+29
-43
lines changed

1 file changed

+29
-43
lines changed

src/Factories/DefinitionGenerator.php

Lines changed: 29 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -2,70 +2,56 @@
22

33
namespace Aerni\Factory\Factories;
44

5+
use Statamic\Fields\Field;
6+
use Statamic\Fields\Fields;
7+
use Statamic\Fields\Blueprint;
58
use Aerni\Factory\Support\Utils;
9+
use Illuminate\Support\Collection;
610
use Illuminate\Contracts\Support\Arrayable;
7-
use Statamic\Fields\Blueprint;
811

912
class DefinitionGenerator implements Arrayable
1013
{
11-
public function __construct(protected Blueprint $blueprint) {}
14+
public function __construct(protected Blueprint $blueprint)
15+
{
16+
}
1217

1318
public function toArray(): array
1419
{
15-
return $this->processBlueprint();
20+
return $this->processFields($this->blueprint->fields()->all());
1621
}
1722

1823
public function __toString(): string
1924
{
2025
return Utils::arrayToString($this->toArray());
2126
}
2227

23-
protected function processBlueprint(): array
24-
{
25-
return $this->blueprint->fields()->all()
26-
->reject(fn ($field) => $field->visibility() === 'computed')
27-
->map(fn ($field) => ['handle' => $field->handle(), 'field' => $field->config()])
28-
->flatMap($this->processFields(...))
29-
->all();
30-
}
31-
32-
protected function processNestedFields(array $fields): array
33-
{
34-
return collect($fields)
35-
->flatMap($this->processFields(...))
36-
->all();
37-
}
38-
39-
protected function processFields(array $config): array
28+
protected function processFields(Collection $fields): array
4029
{
41-
return match (true) {
42-
$config['field']['type'] === 'bard' => $this->processBardAndReplicator($config),
43-
$config['field']['type'] === 'replicator' => $this->processBardAndReplicator($config),
44-
$config['field']['type'] === 'grid' => $this->processGrid($config),
45-
default => [$config['handle'] => null]
46-
};
30+
return $fields->map(fn (Field $field) => match ($field->type()) {
31+
'bard' => $this->processBardAndReplicator($field),
32+
'replicator' => $this->processBardAndReplicator($field),
33+
'grid' => $this->processGrid($field),
34+
default => null,
35+
})->all();
4736
}
4837

49-
protected function processBardAndReplicator(array $config): array
38+
protected function processBardAndReplicator(Field $field): array
5039
{
51-
$fields = collect($config['field']['sets'])->flatMap(function ($group) {
52-
return collect($group['sets'])->map(function ($set, $key) {
53-
return array_merge($this->processNestedFields($set['fields']), [
54-
'type' => $key,
55-
'enabled' => true,
56-
]);
57-
});
58-
})->values()->all();
59-
60-
return [$config['handle'] => $fields];
40+
return collect($field->toArray()['sets'])
41+
->flatMap(function ($setGroup) {
42+
return collect($setGroup['sets'])->map(function ($set, $type) {
43+
return array_merge(
44+
$this->processFields((new Fields($set['fields']))->all()),
45+
['type' => $type, 'enabled' => true]
46+
);
47+
});
48+
})->values()->toArray();
6149
}
6250

63-
protected function processGrid(array $config): array
51+
protected function processGrid(Field $field): array
6452
{
65-
$fields = collect($config['field']['fields'])
66-
->flatMap(fn ($config) => $this->processNestedFields([$config]))
67-
->toArray();
68-
69-
return [$config['handle'] => $fields];
53+
return (new Fields($field->toArray()['fields']))
54+
->all()
55+
->pipe($this->processFields(...));
7056
}
7157
}

0 commit comments

Comments
 (0)