From ba0f89e2f7af449a1236cc45353dc62abd9656aa Mon Sep 17 00:00:00 2001 From: Jack Sleight Date: Mon, 27 Nov 2023 16:37:13 +0000 Subject: [PATCH 1/4] Add fieldPathPrefix to PHP field classes --- .../js/components/fieldtypes/Fieldtype.vue | 6 ++++ .../fieldtypes/RevealerFieldtype.vue | 2 +- src/Fields/Field.php | 29 +++++++++++++-- src/Fields/Fields.php | 14 ++++---- src/Fieldtypes/Bard.php | 24 ++++++------- src/Fieldtypes/Grid.php | 36 +++++++++---------- src/Fieldtypes/Replicator.php | 35 +++++++++--------- 7 files changed, 88 insertions(+), 58 deletions(-) diff --git a/resources/js/components/fieldtypes/Fieldtype.vue b/resources/js/components/fieldtypes/Fieldtype.vue index 00b50f441e6..d28a685569a 100644 --- a/resources/js/components/fieldtypes/Fieldtype.vue +++ b/resources/js/components/fieldtypes/Fieldtype.vue @@ -59,6 +59,12 @@ export default { return this.value; }, + fieldPath() { + const prefix = this.fieldPathPrefix || this.handle; + + return prefix.split('.'); + }, + fieldId() { let prefix = this.fieldPathPrefix ? this.fieldPathPrefix+'.' : ''; diff --git a/resources/js/components/fieldtypes/RevealerFieldtype.vue b/resources/js/components/fieldtypes/RevealerFieldtype.vue index 830c1a4cc42..33d717780eb 100644 --- a/resources/js/components/fieldtypes/RevealerFieldtype.vue +++ b/resources/js/components/fieldtypes/RevealerFieldtype.vue @@ -35,7 +35,7 @@ export default { return data_get(this.config, 'mode') === 'toggle'; }, - fieldPath() { + prefix() { return this.fieldPathPrefix || this.handle; }, diff --git a/src/Fields/Field.php b/src/Fields/Field.php index bf9bee6e5db..84bcb89e653 100644 --- a/src/Fields/Field.php +++ b/src/Fields/Field.php @@ -20,6 +20,7 @@ class Field implements Arrayable protected $value; protected $parent; protected $parentField; + protected $parentIndex; protected $validationContext; protected ?Form $form = null; @@ -33,7 +34,7 @@ public function newInstance() { return (new static($this->handle, $this->config)) ->setParent($this->parent) - ->setParentField($this->parentField) + ->setParentField($this->parentField, $this->parentIndex) ->setValue($this->value); } @@ -58,6 +59,24 @@ public function handlePath() return $path; } + public function fieldPath() + { + $path = $this->parentField ? $this->parentField->fieldPath() : []; + + if (isset($this->parentIndex)) { + $path[] = $this->parentIndex; + } + + $path[] = $this->handle(); + + return $path; + } + + public function fieldPathPrefix() + { + return implode('.', $this->fieldPath()); + } + public function setPrefix($prefix) { $this->prefix = $prefix; @@ -70,6 +89,11 @@ public function prefix() return $this->prefix; } + public function parentIndex() + { + return $this->parentIndex; + } + public function type() { return array_get($this->config, 'type', 'text'); @@ -277,9 +301,10 @@ public function parent() return $this->parent; } - public function setParentField($field) + public function setParentField($field, $index = null) { $this->parentField = $field; + $this->parentIndex = $index; return $this; } diff --git a/src/Fields/Fields.php b/src/Fields/Fields.php index c3f34dd7db6..43697aa667f 100644 --- a/src/Fields/Fields.php +++ b/src/Fields/Fields.php @@ -15,15 +15,16 @@ class Fields protected $fields; protected $parent; protected $parentField; + protected $parentIndex; protected $filled = []; protected $withValidatableValues = false; protected $withComputedValues = false; - public function __construct($items = [], $parent = null, $parentField = null) + public function __construct($items = [], $parent = null, $parentField = null, $parentIndex = null) { $this ->setParent($parent) - ->setParentField($parentField) + ->setParentField($parentField, $parentIndex) ->setItems($items); } @@ -58,12 +59,13 @@ public function setParent($parent) return $this; } - public function setParentField($field) + public function setParentField($field, $index = null) { $this->parentField = $field; + $this->parentIndex = $index; if ($this->fields) { - $this->fields->each(fn ($f) => $f->setParentField($field)); + $this->fields->each(fn ($f) => $f->setParentField($field, $index)); } return $this; @@ -114,7 +116,7 @@ public function newInstance() { return (new static) ->setParent($this->parent) - ->setParentField($this->parentField) + ->setParentField($this->parentField, $this->parentIndex) ->setItems($this->items) ->setFields($this->fields) ->setFilled($this->filled); @@ -244,7 +246,7 @@ protected function newField($handle, $config) { return (new Field($handle, $config)) ->setParent($this->parent) - ->setParentField($this->parentField); + ->setParentField($this->parentField, $this->parentIndex); } private function getReferencedField(array $config): Field diff --git a/src/Fieldtypes/Bard.php b/src/Fieldtypes/Bard.php index 9a768f3f79d..6a17d1c8f0a 100644 --- a/src/Fieldtypes/Bard.php +++ b/src/Fieldtypes/Bard.php @@ -9,7 +9,6 @@ use Statamic\Facades\Entry; use Statamic\Facades\GraphQL; use Statamic\Facades\Site; -use Statamic\Fields\Fields; use Statamic\Fieldtypes\Bard\Augmentor; use Statamic\GraphQL\Types\BardSetsType; use Statamic\GraphQL\Types\BardTextType; @@ -265,12 +264,12 @@ public function process($value) $value = $this->unwrapInlineValue($value); } - $structure = collect($value)->map(function ($row) { + $structure = collect($value)->map(function ($row, $index) { if ($row['type'] !== 'set') { return $row; } - return $this->processRow($row); + return $this->processRow($row, $index); })->all(); if ($this->shouldSaveHtml()) { @@ -334,9 +333,9 @@ protected function shouldSaveHtml() return $this->config('save_html'); } - protected function processRow($row) + protected function processRow($row, $index) { - $row['attrs']['values'] = parent::processRow($row['attrs']['values']); + $row['attrs']['values'] = parent::processRow($row['attrs']['values'], $index); if (array_get($row, 'attrs.enabled', true) === true) { unset($row['attrs']['enabled']); @@ -532,21 +531,20 @@ public function preload() $existing = collect($value)->filter(function ($item) { return $item['type'] === 'set'; - })->mapWithKeys(function ($set) { + })->mapWithKeys(function ($set, $index) { $values = $set['attrs']['values']; - $config = Arr::get($this->flattenedSetsConfig(), "{$values['type']}.fields", []); - return [$set['attrs']['id'] => (new Fields($config))->addValues($values)->meta()->put('_', '_')]; + return [$set['attrs']['id'] => $this->fields($set['type'], $index)->addValues($values)->meta()->put('_', '_')]; })->toArray(); - $defaults = collect($this->flattenedSetsConfig())->map(function ($set) { - return (new Fields($set['fields']))->all()->map(function ($field) { + $defaults = collect($this->flattenedSetsConfig())->map(function ($set, $handle) { + return $this->fields($handle, -1)->all()->map(function ($field) { return $field->fieldtype()->preProcess($field->defaultValue()); })->all(); })->all(); $new = collect($this->flattenedSetsConfig())->map(function ($set, $handle) use ($defaults) { - return (new Fields($set['fields']))->addValues($defaults[$handle])->meta()->put('_', '_'); + return $this->fields($handle, -1)->addValues($defaults[$handle])->meta()->put('_', '_'); })->toArray(); $previews = collect($existing)->map(function ($fields) { @@ -587,14 +585,14 @@ public function preProcessValidatable($value) $value = json_decode($value ?? '[]', true); - return collect($value)->map(function ($item) { + return collect($value)->map(function ($item, $index) { if ($item['type'] !== 'set') { return $item; } $values = $item['attrs']['values']; - $processed = $this->fields($values['type']) + $processed = $this->fields($values['type'], $index) ->addValues($values) ->preProcessValidatables() ->values() diff --git a/src/Fieldtypes/Grid.php b/src/Fieldtypes/Grid.php index e07dc02fd86..3d8e6f24437 100644 --- a/src/Fieldtypes/Grid.php +++ b/src/Fieldtypes/Grid.php @@ -84,14 +84,14 @@ public function filter() public function process($data) { - return collect($data)->map(function ($row) { - return $this->processRow($row); + return collect($data)->map(function ($row, $index) { + return $this->processRow($row, $index); })->all(); } - private function processRow($row) + private function processRow($row, $index) { - $fields = $this->fields()->addValues($row)->process()->values()->all(); + $fields = $this->fields($index)->addValues($row)->process()->values()->all(); $row = array_merge([RowId::handle() => Arr::pull($row, '_id')], $row, $fields); @@ -113,7 +113,7 @@ public function preProcess($data) private function preProcessRow($row, $index) { - $fields = $this->fields()->addValues($row)->preProcess()->values()->all(); + $fields = $this->fields($index)->addValues($row)->preProcess()->values()->all(); $id = Arr::pull($row, RowId::handle()) ?? RowId::generate(); @@ -122,9 +122,9 @@ private function preProcessRow($row, $index) ]); } - public function fields() + public function fields($index) { - return new Fields($this->config('fields'), $this->field()->parent(), $this->field()); + return new Fields($this->config('fields'), $this->field()->parent(), $this->field(), $index); } public function rules(): array @@ -154,7 +154,7 @@ public function extraRules(): array protected function rowRules($data, $index) { $rules = $this - ->fields() + ->fields($index) ->addValues($data) ->validator() ->withContext([ @@ -174,9 +174,9 @@ protected function rowRuleFieldPrefix($index) public function extraValidationAttributes(): array { - $attributes = $this->fields()->validator()->attributes(); + return collect($this->field->value())->map(function ($row, $index) { + $attributes = $this->fields($index)->validator()->attributes(); - return collect($this->field->value())->map(function ($row, $index) use ($attributes) { return collect($attributes)->except('_id')->mapWithKeys(function ($attribute, $handle) use ($index) { return [$this->rowRuleFieldPrefix($index).'.'.$handle => $attribute]; }); @@ -189,16 +189,16 @@ public function preload() { return [ 'defaults' => $this->defaultRowData()->all(), - 'new' => $this->fields()->meta()->all(), - 'existing' => collect($this->field->value())->mapWithKeys(function ($row) { - return [$row['_id'] => $this->fields()->addValues($row)->meta()]; + 'new' => $this->fields(-1)->meta()->all(), + 'existing' => collect($this->field->value())->mapWithKeys(function ($row, $index) { + return [$row['_id'] => $this->fields($index)->addValues($row)->meta()]; })->toArray(), ]; } protected function defaultRowData() { - return $this->fields()->all()->map(function ($field) { + return $this->fields(-1)->all()->map(function ($field) { return $field->fieldtype()->preProcess($field->defaultValue()); }); } @@ -217,8 +217,8 @@ private function performAugmentation($value, $shallow) { $method = $shallow ? 'shallowAugment' : 'augment'; - return collect($value)->map(function ($row) use ($method) { - $values = $this->fields()->addValues($row)->{$method}()->values(); + return collect($value)->map(function ($row, $index) use ($method) { + $values = $this->fields($index)->addValues($row)->{$method}()->values(); return new Values($values->merge([RowId::handle() => $row[RowId::handle()] ?? null])->all()); })->all(); @@ -247,8 +247,8 @@ private function gqlItemTypeName() public function preProcessValidatable($value) { - return collect($value)->map(function ($values) { - $processed = $this->fields() + return collect($value)->map(function ($values, $index) { + $processed = $this->fields($index) ->addValues($values) ->preProcessValidatables() ->values() diff --git a/src/Fieldtypes/Replicator.php b/src/Fieldtypes/Replicator.php index 9c502e15f2c..e2b17104236 100644 --- a/src/Fieldtypes/Replicator.php +++ b/src/Fieldtypes/Replicator.php @@ -78,14 +78,14 @@ public function filter() public function process($data) { - return collect($data)->map(function ($row) { - return $this->processRow($row); + return collect($data)->map(function ($row, $i) { + return $this->processRow($row, $i); })->all(); } - protected function processRow($row) + protected function processRow($row, $index) { - $fields = $this->fields($row['type'])->addValues($row)->process()->values()->all(); + $fields = $this->fields($row['type'], $index)->addValues($row)->process()->values()->all(); $row = array_merge([RowId::handle() => Arr::pull($row, '_id')], $row, $fields); @@ -101,7 +101,7 @@ public function preProcess($data) protected function preProcessRow($row, $index) { - $fields = $this->fields($row['type'])->addValues($row)->preProcess()->values()->all(); + $fields = $this->fields($row['type'], $index)->addValues($row)->preProcess()->values()->all(); $id = Arr::pull($row, RowId::handle()) ?? RowId::generate(); @@ -111,12 +111,13 @@ protected function preProcessRow($row, $index) ]); } - public function fields($set) + public function fields($set, $index) { return new Fields( Arr::get($this->flattenedSetsConfig(), "$set.fields"), $this->field()->parent(), - $this->field() + $this->field(), + $index ); } @@ -132,7 +133,7 @@ public function extraRules(): array protected function setRules($handle, $data, $index) { $rules = $this - ->fields($handle) + ->fields($handle, $index) ->addValues($data) ->validator() ->withContext([ @@ -161,7 +162,7 @@ public function extraValidationAttributes(): array protected function setValidationAttributes($handle, $data, $index) { - $attributes = $this->fields($handle)->addValues($data)->validator()->attributes(); + $attributes = $this->fields($handle, $index)->addValues($data)->validator()->attributes(); return collect($attributes)->mapWithKeys(function ($attribute, $handle) use ($index) { return [$this->setRuleFieldPrefix($index).'.'.$handle => $attribute]; @@ -197,20 +198,18 @@ protected function performAugmentation($values, $shallow) public function preload() { - $existing = collect($this->field->value())->mapWithKeys(function ($set) { - $config = Arr::get($this->flattenedSetsConfig(), "{$set['type']}.fields", []); - - return [$set['_id'] => (new Fields($config))->addValues($set)->meta()->put('_', '_')]; + $existing = collect($this->field->value())->mapWithKeys(function ($set, $index) { + return [$set['_id'] => $this->fields($set['type'], $index)->addValues($set)->meta()->put('_', '_')]; })->toArray(); - $defaults = collect($this->flattenedSetsConfig())->map(function ($set) { - return (new Fields($set['fields']))->all()->map(function ($field) { + $defaults = collect($this->flattenedSetsConfig())->map(function ($set, $handle) { + return $this->fields($handle, -1)->all()->map(function ($field) { return $field->fieldtype()->preProcess($field->defaultValue()); })->all(); })->all(); $new = collect($this->flattenedSetsConfig())->map(function ($set, $handle) use ($defaults) { - return (new Fields($set['fields']))->addValues($defaults[$handle])->meta()->put('_', '_'); + return $this->fields($handle, -1)->addValues($defaults[$handle])->meta()->put('_', '_'); })->toArray(); $previews = collect($existing)->map(function ($fields) { @@ -294,8 +293,8 @@ protected function gqlSetsTypeName() public function preProcessValidatable($value) { - return collect($value)->map(function ($values) { - $processed = $this->fields($values['type']) + return collect($value)->map(function ($values, $index) { + $processed = $this->fields($values['type'], $index) ->addValues($values) ->preProcessValidatables() ->values() From 3eb927a847f41bdf218e5e5523d472d7ce0887ac Mon Sep 17 00:00:00 2001 From: Jack Sleight Date: Tue, 28 Nov 2023 10:33:17 +0000 Subject: [PATCH 2/4] WIP --- resources/js/components/fieldtypes/Fieldtype.vue | 2 +- resources/js/components/fieldtypes/RevealerFieldtype.vue | 2 +- src/Fields/Field.php | 6 +++--- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/resources/js/components/fieldtypes/Fieldtype.vue b/resources/js/components/fieldtypes/Fieldtype.vue index d28a685569a..fb0f542c06c 100644 --- a/resources/js/components/fieldtypes/Fieldtype.vue +++ b/resources/js/components/fieldtypes/Fieldtype.vue @@ -59,7 +59,7 @@ export default { return this.value; }, - fieldPath() { + fieldPathKeys() { const prefix = this.fieldPathPrefix || this.handle; return prefix.split('.'); diff --git a/resources/js/components/fieldtypes/RevealerFieldtype.vue b/resources/js/components/fieldtypes/RevealerFieldtype.vue index 33d717780eb..830c1a4cc42 100644 --- a/resources/js/components/fieldtypes/RevealerFieldtype.vue +++ b/resources/js/components/fieldtypes/RevealerFieldtype.vue @@ -35,7 +35,7 @@ export default { return data_get(this.config, 'mode') === 'toggle'; }, - prefix() { + fieldPath() { return this.fieldPathPrefix || this.handle; }, diff --git a/src/Fields/Field.php b/src/Fields/Field.php index 84bcb89e653..04a46c84899 100644 --- a/src/Fields/Field.php +++ b/src/Fields/Field.php @@ -59,9 +59,9 @@ public function handlePath() return $path; } - public function fieldPath() + public function fieldPathKeys() { - $path = $this->parentField ? $this->parentField->fieldPath() : []; + $path = $this->parentField ? $this->parentField->fieldPathKeys() : []; if (isset($this->parentIndex)) { $path[] = $this->parentIndex; @@ -74,7 +74,7 @@ public function fieldPath() public function fieldPathPrefix() { - return implode('.', $this->fieldPath()); + return implode('.', $this->fieldPathKeys()); } public function setPrefix($prefix) From 5986b4cb8cfb0893c388b8dcfc1612769d13cc42 Mon Sep 17 00:00:00 2001 From: Jack Sleight Date: Tue, 28 Nov 2023 10:35:34 +0000 Subject: [PATCH 3/4] WIP --- src/Fieldtypes/Bard.php | 4 ++-- src/Fieldtypes/Grid.php | 6 +++--- src/Fieldtypes/Replicator.php | 6 +++--- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/Fieldtypes/Bard.php b/src/Fieldtypes/Bard.php index 6a17d1c8f0a..8ced200a5bb 100644 --- a/src/Fieldtypes/Bard.php +++ b/src/Fieldtypes/Bard.php @@ -538,13 +538,13 @@ public function preload() })->toArray(); $defaults = collect($this->flattenedSetsConfig())->map(function ($set, $handle) { - return $this->fields($handle, -1)->all()->map(function ($field) { + 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, -1)->addValues($defaults[$handle])->meta()->put('_', '_'); + return $this->fields($handle)->addValues($defaults[$handle])->meta()->put('_', '_'); })->toArray(); $previews = collect($existing)->map(function ($fields) { diff --git a/src/Fieldtypes/Grid.php b/src/Fieldtypes/Grid.php index 3d8e6f24437..5f4783d5862 100644 --- a/src/Fieldtypes/Grid.php +++ b/src/Fieldtypes/Grid.php @@ -122,7 +122,7 @@ private function preProcessRow($row, $index) ]); } - public function fields($index) + public function fields($index = -1) { return new Fields($this->config('fields'), $this->field()->parent(), $this->field(), $index); } @@ -189,7 +189,7 @@ public function preload() { return [ 'defaults' => $this->defaultRowData()->all(), - 'new' => $this->fields(-1)->meta()->all(), + 'new' => $this->fields()->meta()->all(), 'existing' => collect($this->field->value())->mapWithKeys(function ($row, $index) { return [$row['_id'] => $this->fields($index)->addValues($row)->meta()]; })->toArray(), @@ -198,7 +198,7 @@ public function preload() protected function defaultRowData() { - return $this->fields(-1)->all()->map(function ($field) { + return $this->fields()->all()->map(function ($field) { return $field->fieldtype()->preProcess($field->defaultValue()); }); } diff --git a/src/Fieldtypes/Replicator.php b/src/Fieldtypes/Replicator.php index e2b17104236..a0bcd688bc4 100644 --- a/src/Fieldtypes/Replicator.php +++ b/src/Fieldtypes/Replicator.php @@ -111,7 +111,7 @@ protected function preProcessRow($row, $index) ]); } - public function fields($set, $index) + public function fields($set, $index = -1) { return new Fields( Arr::get($this->flattenedSetsConfig(), "$set.fields"), @@ -203,13 +203,13 @@ public function preload() })->toArray(); $defaults = collect($this->flattenedSetsConfig())->map(function ($set, $handle) { - return $this->fields($handle, -1)->all()->map(function ($field) { + 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, -1)->addValues($defaults[$handle])->meta()->put('_', '_'); + return $this->fields($handle)->addValues($defaults[$handle])->meta()->put('_', '_'); })->toArray(); $previews = collect($existing)->map(function ($fields) { From 19c15a2313f62df880476ab47b21aec9556de874 Mon Sep 17 00:00:00 2001 From: Jack Sleight Date: Tue, 28 Nov 2023 11:41:44 +0000 Subject: [PATCH 4/4] WIP --- src/Fieldtypes/Bard.php | 2 +- src/Fieldtypes/Bard/Augmentor.php | 4 +- src/Fieldtypes/Replicator.php | 4 +- tests/Fields/FieldsTest.php | 20 +++++ tests/Fieldtypes/BardTest.php | 120 ++++++++++++++++++++++++++++ tests/Fieldtypes/GridTest.php | 72 +++++++++++++++++ tests/Fieldtypes/ReplicatorTest.php | 78 ++++++++++++++++++ 7 files changed, 295 insertions(+), 5 deletions(-) diff --git a/src/Fieldtypes/Bard.php b/src/Fieldtypes/Bard.php index 8ced200a5bb..8f60fa74ccf 100644 --- a/src/Fieldtypes/Bard.php +++ b/src/Fieldtypes/Bard.php @@ -534,7 +534,7 @@ public function preload() })->mapWithKeys(function ($set, $index) { $values = $set['attrs']['values']; - return [$set['attrs']['id'] => $this->fields($set['type'], $index)->addValues($values)->meta()->put('_', '_')]; + return [$set['attrs']['id'] => $this->fields($values['type'], $index)->addValues($values)->meta()->put('_', '_')]; })->toArray(); $defaults = collect($this->flattenedSetsConfig())->map(function ($set, $handle) { diff --git a/src/Fieldtypes/Bard/Augmentor.php b/src/Fieldtypes/Bard/Augmentor.php index 84aeec923f5..1b0e037f973 100644 --- a/src/Fieldtypes/Bard/Augmentor.php +++ b/src/Fieldtypes/Bard/Augmentor.php @@ -149,12 +149,12 @@ protected function augmentSets($value, $shallow) { $augmentMethod = $shallow ? 'shallowAugment' : 'augment'; - return $value->map(function ($set) use ($augmentMethod) { + return $value->map(function ($set, $index) use ($augmentMethod) { if (! Arr::get($this->fieldtype->flattenedSetsConfig(), "{$set['type']}.fields")) { return $set; } - $values = $this->fieldtype->fields($set['type'])->addValues($set)->{$augmentMethod}()->values()->all(); + $values = $this->fieldtype->fields($set['type'], $index)->addValues($set)->{$augmentMethod}()->values()->all(); return array_merge($values, [RowId::handle() => $set[RowId::handle()] ?? null, 'type' => $set['type']]); })->all(); diff --git a/src/Fieldtypes/Replicator.php b/src/Fieldtypes/Replicator.php index a0bcd688bc4..a437022e073 100644 --- a/src/Fieldtypes/Replicator.php +++ b/src/Fieldtypes/Replicator.php @@ -183,14 +183,14 @@ protected function performAugmentation($values, $shallow) { return collect($values)->reject(function ($set, $key) { return array_get($set, 'enabled', true) === false; - })->map(function ($set) use ($shallow) { + })->map(function ($set, $index) use ($shallow) { if (! Arr::get($this->flattenedSetsConfig(), "{$set['type']}.fields")) { return $set; } $augmentMethod = $shallow ? 'shallowAugment' : 'augment'; - $values = $this->fields($set['type'])->addValues($set)->{$augmentMethod}()->values(); + $values = $this->fields($set['type'], $index)->addValues($set)->{$augmentMethod}()->values(); return new Values($values->merge([RowId::handle() => $set[RowId::handle()] ?? null, 'type' => $set['type']])->all()); })->values()->all(); diff --git a/tests/Fields/FieldsTest.php b/tests/Fields/FieldsTest.php index b7bd1285224..7983695009a 100644 --- a/tests/Fields/FieldsTest.php +++ b/tests/Fields/FieldsTest.php @@ -983,4 +983,24 @@ public function it_sets_the_parentfield_on_all_fields() $this->assertEquals('foo', $collection['one']->parentField()); $this->assertEquals('foo', $collection['two']->parentField()); } + + /** + * @test + */ + public function it_sets_the_parentindex_on_all_fields() + { + $fields = new Fields([ + ['handle' => 'one', 'field' => ['type' => 'text']], + ['handle' => 'two', 'field' => ['type' => 'text']], + ]); + + $collection = $fields->all(); + $this->assertNull($collection['one']->parentIndex()); + $this->assertNull($collection['two']->parentIndex()); + + $fields->setParentField('foo', 1); + $collection = $fields->all(); + $this->assertEquals(1, $collection['one']->parentIndex()); + $this->assertEquals(1, $collection['two']->parentIndex()); + } } diff --git a/tests/Fieldtypes/BardTest.php b/tests/Fieldtypes/BardTest.php index fbb8c90dd50..78fa2c621d3 100644 --- a/tests/Fieldtypes/BardTest.php +++ b/tests/Fieldtypes/BardTest.php @@ -1091,6 +1091,126 @@ public function it_converts_tiptap_v1_snake_case_types_to_v2_camel_case_types() $this->assertEquals($expected, json_decode($this->bard()->preProcess($data), true)); } + /** + * @test + * + * @dataProvider groupedSetsProvider + */ + public function it_generates_field_path_prefix($areSetsGrouped) + { + $fieldtype = new class extends Fieldtype + { + public static function handle() + { + return 'custom'; + } + + public function preProcess($value) + { + return $this->field()->fieldPathPrefix(); + } + + public function process($value) + { + return $this->field()->fieldPathPrefix(); + } + + public function preload() + { + return ['fieldPathPrefix' => $this->field()->fieldPathPrefix()]; + } + + public function augment($value) + { + return $this->field()->fieldPathPrefix(); + } + }; + + $fieldtype::register(); + + $field = (new Field('test', [ + 'type' => 'bard', + 'sets' => $this->groupSets($areSetsGrouped, [ + 'one' => [ + 'fields' => [ + ['handle' => 'words', 'field' => ['type' => 'custom']], + ], + ], + ]), + ]))->setValue([ + [ + 'type' => 'set', + 'attrs' => [ + 'id' => 'set-id-1', + 'values' => [ + 'type' => 'one', + 'words' => 'test', + ], + ], + ], + [ + 'type' => 'set', + 'attrs' => [ + 'id' => 'set-id-2', + 'values' => [ + 'type' => 'one', + 'words' => 'test', + ], + ], + ], + ]); + + $value = $field->augment()->value()->value(); + $this->assertEquals('test.0.words', $value[0]['words']); + $this->assertEquals('test.1.words', $value[1]['words']); + + $value = json_decode($field->preProcess()->value(), true); + $this->assertEquals('test.0.words', $value[0]['attrs']['values']['words']); + $this->assertEquals('test.1.words', $value[1]['attrs']['values']['words']); + + $field = (new Field('test', [ + 'type' => 'bard', + 'sets' => $this->groupSets($areSetsGrouped, [ + 'one' => [ + 'fields' => [ + ['handle' => 'words', 'field' => ['type' => 'custom']], + ], + ], + ]), + ]))->setValue(json_encode([ + [ + 'type' => 'set', + 'attrs' => [ + 'id' => 'set-id-1', + 'values' => [ + 'type' => 'one', + 'words' => 'test', + ], + ], + ], + [ + 'type' => 'set', + 'attrs' => [ + 'id' => 'set-id-2', + 'values' => [ + 'type' => 'one', + 'words' => 'test', + ], + ], + ], + ])); + + $value = $field->process()->value(); + $this->assertEquals('test.0.words', $value[0]['attrs']['values']['words']); + $this->assertEquals('test.1.words', $value[1]['attrs']['values']['words']); + + $value = $field->fieldtype()->preload(); + $this->assertEquals('test.0.words', $value['existing']['set-id-1']['words']['fieldPathPrefix']); + $this->assertEquals('test.1.words', $value['existing']['set-id-2']['words']['fieldPathPrefix']); + $this->assertEquals('test.-1.words', $value['new']['one']['words']['fieldPathPrefix']); + $this->assertEquals('test.-1.words', $value['defaults']['one']['words']); + } + private function bard($config = []) { return (new Bard)->setField(new Field('test', array_merge(['type' => 'bard', 'sets' => ['one' => []]], $config))); diff --git a/tests/Fieldtypes/GridTest.php b/tests/Fieldtypes/GridTest.php index 61be84be2ad..233b3cd73f3 100644 --- a/tests/Fieldtypes/GridTest.php +++ b/tests/Fieldtypes/GridTest.php @@ -527,4 +527,76 @@ public function it_converts_a_queryable_value() $this->assertNull((new Grid)->toQueryableValue([])); $this->assertEquals([['foo' => 'bar']], (new Grid)->toQueryableValue([['foo' => 'bar']])); } + + /** + * @test + */ + public function it_generates_field_path_prefix() + { + $fieldtype = new class extends Fieldtype + { + public static function handle() + { + return 'custom'; + } + + public function preProcess($value) + { + return $this->field()->fieldPathPrefix(); + } + + public function process($value) + { + return $this->field()->fieldPathPrefix(); + } + + public function preload() + { + return ['fieldPathPrefix' => $this->field()->fieldPathPrefix()]; + } + + public function augment($value) + { + return $this->field()->fieldPathPrefix(); + } + }; + + $fieldtype::register(); + + $field = (new Field('test', [ + 'type' => 'grid', + 'fields' => [ + ['handle' => 'words', 'field' => ['type' => 'custom']], + ], + ]))->setValue([ + [ + '_id' => 'set-id-1', + 'type' => 'one', + 'words' => 'test', + ], + [ + '_id' => 'set-id-2', + 'type' => 'one', + 'words' => 'test', + ], + ]); + + $value = $field->augment()->value()->value(); + $this->assertEquals('test.0.words', $value[0]['words']); + $this->assertEquals('test.1.words', $value[1]['words']); + + $value = $field->preProcess()->value(); + $this->assertEquals('test.0.words', $value[0]['words']); + $this->assertEquals('test.1.words', $value[1]['words']); + + $value = $field->process()->value(); + $this->assertEquals('test.0.words', $value[0]['words']); + $this->assertEquals('test.1.words', $value[1]['words']); + + $value = $field->fieldtype()->preload(); + $this->assertEquals('test.0.words', $value['existing']['set-id-1']['words']['fieldPathPrefix']); + $this->assertEquals('test.1.words', $value['existing']['set-id-2']['words']['fieldPathPrefix']); + $this->assertEquals('test.-1.words', $value['new']['words']['fieldPathPrefix']); + $this->assertEquals('test.-1.words', $value['defaults']['words']); + } } diff --git a/tests/Fieldtypes/ReplicatorTest.php b/tests/Fieldtypes/ReplicatorTest.php index a86bf5f1dd0..64f13a86af3 100644 --- a/tests/Fieldtypes/ReplicatorTest.php +++ b/tests/Fieldtypes/ReplicatorTest.php @@ -613,6 +613,84 @@ public function it_converts_a_queryable_value() $this->assertEquals([['foo' => 'bar']], (new Replicator)->toQueryableValue([['foo' => 'bar']])); } + /** + * @test + * + * @dataProvider groupedSetsProvider + */ + public function it_generates_field_path_prefix($areSetsGrouped) + { + $fieldtype = new class extends Fieldtype + { + public static function handle() + { + return 'custom'; + } + + public function preProcess($value) + { + return $this->field()->fieldPathPrefix(); + } + + public function process($value) + { + return $this->field()->fieldPathPrefix(); + } + + public function preload() + { + return ['fieldPathPrefix' => $this->field()->fieldPathPrefix()]; + } + + public function augment($value) + { + return $this->field()->fieldPathPrefix(); + } + }; + + $fieldtype::register(); + + $field = (new Field('test', [ + 'type' => 'replicator', + 'sets' => $this->groupSets($areSetsGrouped, [ + 'one' => [ + 'fields' => [ + ['handle' => 'words', 'field' => ['type' => 'custom']], + ], + ], + ]), + ]))->setValue([ + [ + '_id' => 'set-id-1', + 'type' => 'one', + 'words' => 'test', + ], + [ + '_id' => 'set-id-2', + 'type' => 'one', + 'words' => 'test', + ], + ]); + + $value = $field->augment()->value()->value(); + $this->assertEquals('test.0.words', $value[0]['words']); + $this->assertEquals('test.1.words', $value[1]['words']); + + $value = $field->preProcess()->value(); + $this->assertEquals('test.0.words', $value[0]['words']); + $this->assertEquals('test.1.words', $value[1]['words']); + + $value = $field->process()->value(); + $this->assertEquals('test.0.words', $value[0]['words']); + $this->assertEquals('test.1.words', $value[1]['words']); + + $value = $field->fieldtype()->preload(); + $this->assertEquals('test.0.words', $value['existing']['set-id-1']['words']['fieldPathPrefix']); + $this->assertEquals('test.1.words', $value['existing']['set-id-2']['words']['fieldPathPrefix']); + $this->assertEquals('test.-1.words', $value['new']['one']['words']['fieldPathPrefix']); + $this->assertEquals('test.-1.words', $value['defaults']['one']['words']); + } + public function groupedSetsProvider() { return [