From 3ac5cd81303a98c134f0af8c78bf123752288955 Mon Sep 17 00:00:00 2001 From: Jason Varga Date: Fri, 11 Mar 2022 17:32:59 -0500 Subject: [PATCH 1/2] Only do it when at the top level (theres no parent field) --- src/Fieldtypes/Terms.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/Fieldtypes/Terms.php b/src/Fieldtypes/Terms.php index ea47e9d0be6..45d1579ca41 100644 --- a/src/Fieldtypes/Terms.php +++ b/src/Fieldtypes/Terms.php @@ -94,7 +94,13 @@ public function augment($values) ->whereIn('id', $ids) ->where('site', $site); - if ($this->usingSingleTaxonomy() && $parent && $parent instanceof Entry && $this->field->handle() === $this->taxonomies()[0]) { + $shouldQueryCollection = $this->usingSingleTaxonomy() + && ! $this->field->parentField() + && $parent + && $parent instanceof Entry + && $this->field->handle() === $this->taxonomies()[0]; + + if ($shouldQueryCollection) { $query->where('collection', $parent->collectionHandle()); } From a61b1cd35f37006a0e6a016815c3bbc03a218ccb Mon Sep 17 00:00:00 2001 From: Jason Varga Date: Mon, 14 Mar 2022 11:48:53 -0400 Subject: [PATCH 2/2] Add test --- tests/Fieldtypes/TermsTest.php | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/tests/Fieldtypes/TermsTest.php b/tests/Fieldtypes/TermsTest.php index 859eb59183c..e7d0bef083d 100644 --- a/tests/Fieldtypes/TermsTest.php +++ b/tests/Fieldtypes/TermsTest.php @@ -338,7 +338,7 @@ public function it_localizes_the_shallow_augmented_item_to_the_current_sites_loc * @test * @dataProvider collectionAttachmentProvider **/ - public function it_attaches_collection_during_augmentation($parentIsEntry, $handle) + public function it_attaches_collection_during_augmentation($parentIsEntry, $handle, $isRootLevel) { // Make sure there is an entry that uses the term. EntryFactory::collection('blog')->data(['tags' => ['one']])->create(); @@ -352,13 +352,17 @@ public function it_attaches_collection_during_augmentation($parentIsEntry, $hand }; } - $fieldtype = $this->fieldtype(['taxonomies' => 'tags'], $parent, $handle); + if (! $isRootLevel) { + $parentField = new Field('grid', ['type' => 'grid']); + } + + $fieldtype = $this->fieldtype(['taxonomies' => 'tags'], $parent, $handle, $parentField ?? null); $augmented = $fieldtype->augment(['one']); $collection = $augmented->first()->collection(); - if ($parentIsEntry && $handle === 'tags') { + if ($parentIsEntry && $handle === 'tags' && $isRootLevel) { $this->assertInstanceOf(CollectionContract::class, $collection); } else { $this->assertNull($collection); @@ -368,10 +372,15 @@ public function it_attaches_collection_during_augmentation($parentIsEntry, $hand public function collectionAttachmentProvider() { return [ - 'parent is entry and handle matches taxonomy' => [true, 'tags'], - 'parent is entry and handle does not match taxonomy' => [true, 'related_tags'], - 'parent is not entry and handle matches taxonomy' => [false, 'tags'], - 'parent is not entry and handle does not match taxonomy' => [false, 'related_tags'], + 'parent is entry and handle matches taxonomy' => [true, 'tags', true], + 'parent is entry and handle does not match taxonomy' => [true, 'related_tags', true], + 'parent is not entry and handle matches taxonomy' => [false, 'tags', true], + 'parent is not entry and handle does not match taxonomy' => [false, 'related_tags', true], + + 'parent is entry, handle matches taxonomy, nested field' => [true, 'tags', false], + 'parent is entry, handle does not match taxonomy, nested field' => [true, 'related_tags', false], + 'parent is not entry, handle matches taxonomy, nested field' => [false, 'tags', false], + 'parent is not entry, handle does not match taxonomy, nested field' => [false, 'related_tags', false], ]; } @@ -391,7 +400,7 @@ public function having_taxonomy_defined_but_not_taxonomies_throws_an_exception() $this->fieldtype(['taxonomy' => 'categories'])->taxonomies(); } - public function fieldtype($config = [], $parent = null, $handle = 'test') + public function fieldtype($config = [], $parent = null, $handle = 'test', $parentField = null) { $field = new Field($handle, array_merge([ 'type' => 'terms', @@ -403,6 +412,10 @@ public function fieldtype($config = [], $parent = null, $handle = 'test') $field->setParent($parent); + if ($parentField) { + $field->setParentField($parentField); + } + return (new Terms)->setField($field); } }