diff --git a/src/Fields/Fields.php b/src/Fields/Fields.php index 56b41bdb0f..d80c256a93 100644 --- a/src/Fields/Fields.php +++ b/src/Fields/Fields.php @@ -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 @@ -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() diff --git a/tests/Fields/FieldsTest.php b/tests/Fields/FieldsTest.php index 2f54f6ee1f..db5ab5a85f 100644 --- a/tests/Fields/FieldsTest.php +++ b/tests/Fields/FieldsTest.php @@ -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()); + } }