Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/Fieldtypes/Terms.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

Expand Down
29 changes: 21 additions & 8 deletions tests/Fieldtypes/TermsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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);
Expand All @@ -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],
];
}

Expand All @@ -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',
Expand All @@ -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);
}
}