Skip to content

Commit

Permalink
[5.x] Fix nested field path prefixes (#10313)
Browse files Browse the repository at this point in the history
  • Loading branch information
jacksleight committed Jun 17, 2024
1 parent f9fe47e commit ec60b17
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 3 deletions.
8 changes: 6 additions & 2 deletions src/Fields/Fields.php
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,8 @@ private function getReferencedField(array $config): Field
$field->setConfig(array_merge($field->config(), $overrides));
}

$field = clone $field;

return $field
->setParent($this->parent)
->setParentField($this->parentField, $this->parentIndex)
Expand Down Expand Up @@ -300,8 +302,10 @@ private function getImportedFields(array $config): array
}

return $fields;
})->each(function ($field) {
$field
})->map(function ($field) {
$field = clone $field;

return $field
->setParent($this->parent)
->setParentField($this->parentField, $this->parentIndex);
})->all();
Expand Down
2 changes: 1 addition & 1 deletion src/Fieldtypes/Replicator.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ protected function preProcessRow($row, $index)
public function fields($set, $index = -1)
{
$config = Arr::get($this->flattenedSetsConfig(), "$set.fields");
$hash = md5($index.json_encode($config));
$hash = md5($this->field->fieldPathPrefix().$index.json_encode($config));

return Blink::once($hash, function () use ($config, $index) {
return new Fields(
Expand Down
97 changes: 97 additions & 0 deletions tests/Fieldtypes/ReplicatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -691,6 +691,103 @@ public function augment($value)
$this->assertEquals('test.-1.words', $value['defaults']['one']['words']);
}

/**
* @test
*
* @dataProvider groupedSetsProvider
*/
public function it_generates_nested_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' => 'nested', 'field' => [
'type' => 'replicator',
'sets' => $this->groupSets($areSetsGrouped, [
'two' => [
'fields' => [
['handle' => 'words', 'field' => ['type' => 'custom']],
],
],
]),
]],
],
],
]),
]))->setValue([
[
'_id' => 'set-id-1',
'type' => 'one',
'nested' => [
[
'_id' => 'nested-set-id-1a',
'type' => 'two',
'words' => 'test',
],
[
'_id' => 'nested-set-id-1b',
'type' => 'two',
'words' => 'test',
],
],
],
[
'_id' => 'set-id-2',
'type' => 'one',
'nested' => [
[
'_id' => 'nested-set-id-2a',
'type' => 'two',
'words' => 'test',
],
[
'_id' => 'nested-set-id-2b',
'type' => 'two',
'words' => 'test',
],
],
],
]);

$value = $field->augment()->value()->value();
$this->assertEquals('test.0.nested.0.words', $value[0]['nested'][0]['words']);
$this->assertEquals('test.0.nested.1.words', $value[0]['nested'][1]['words']);
$this->assertEquals('test.1.nested.0.words', $value[1]['nested'][0]['words']);
$this->assertEquals('test.1.nested.1.words', $value[1]['nested'][1]['words']);
}

public static function groupedSetsProvider()
{
return [
Expand Down

0 comments on commit ec60b17

Please sign in to comment.