Skip to content

Commit

Permalink
[4.x] Pass parent field and index down to imported fields (#9550)
Browse files Browse the repository at this point in the history
  • Loading branch information
jacksleight authored Feb 20, 2024
1 parent 693762c commit 8c2a481
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/Fields/Fields.php
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,10 @@ private function getReferencedField(array $config): Field
$field->setConfig(array_merge($field->config(), $overrides));
}

return $field->setParent($this->parent)->setHandle($config['handle']);
return $field
->setParent($this->parent)
->setParentField($this->parentField, $this->parentIndex)
->setHandle($config['handle']);
}

private function getImportedFields(array $config): array
Expand Down Expand Up @@ -291,7 +294,11 @@ private function getImportedFields(array $config): array
}

return $fields;
})->each->setParent($this->parent)->all();
})->each(function ($field) {
$field
->setParent($this->parent)
->setParentField($this->parentField, $this->parentIndex);
})->all();
}

public function meta()
Expand Down
54 changes: 54 additions & 0 deletions tests/Fields/FieldsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1003,4 +1003,58 @@ public function it_sets_the_parentindex_on_all_fields()
$this->assertEquals(1, $collection['one']->parentIndex());
$this->assertEquals(1, $collection['two']->parentIndex());
}

/**
* @test
*/
public function it_sets_the_parentfield_and_parentindex_on_imported_fields()
{
$fieldset = (new Fieldset)->setHandle('partial')->setContents([
'fields' => [
['handle' => 'bar', 'field' => ['type' => 'text']],
],
]);

FieldsetRepository::shouldReceive('find')->with('partial')->once()->andReturn($fieldset);

$parentField = new Field('foo', ['type' => 'replicator']);

$fields = new Fields(
[['import' => 'partial']],
null,
$parentField,
1,
);

$collection = $fields->all();
$this->assertEquals($parentField, $collection['bar']->parentField());
$this->assertEquals(1, $collection['bar']->parentIndex());
}

/**
* @test
*/
public function it_sets_the_parentfield_and_parentindex_on_referenced_fields()
{
$fieldset = (new Fieldset)->setHandle('partial')->setContents([
'fields' => [
['handle' => 'bar', 'field' => ['type' => 'text']],
],
]);

FieldsetRepository::shouldReceive('find')->with('partial')->once()->andReturn($fieldset);

$parentField = new Field('foo', ['type' => 'replicator']);

$fields = new Fields(
[['handle' => 'bar', 'field' => 'partial.bar']],
null,
$parentField,
1,
);

$collection = $fields->all();
$this->assertEquals($parentField, $collection['bar']->parentField());
$this->assertEquals(1, $collection['bar']->parentIndex());
}
}

0 comments on commit 8c2a481

Please sign in to comment.