Skip to content

Commit 92d484b

Browse files
authored
[6.x] Cache term augmentation with taxonomy-qualified ID (#15974)
1 parent 049a7e3 commit 92d484b

2 files changed

Lines changed: 24 additions & 4 deletions

File tree

‎src/Fieldtypes/Terms.php‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Statamic\Fieldtypes;
44

5+
use Closure;
6+
use Exception;
57
use Illuminate\Http\Resources\Json\JsonResource;
68
use Illuminate\Support\Collection;
79
use Statamic\Contracts\Data\Localization;
@@ -158,7 +160,11 @@ public function augment($values)
158160
? $parent->locale()
159161
: Site::current()->handle(); // Use the "current" site so this will get localized appropriately on the front-end.
160162

161-
if ($single && Blink::has($key = 'terms-augment-'.$site.'-'.json_encode($values))) {
163+
// Use the taxonomy-qualified IDs in the cache key, since a value stored by a field
164+
// limited to a single taxonomy will not contain the taxonomy handle on its own.
165+
$key = 'terms-augment-'.$site.'-'.json_encode($this->convertAugmentationValuesToIds($values));
166+
167+
if ($single && Blink::has($key)) {
162168
return Blink::get($key);
163169
}
164170

@@ -219,7 +225,7 @@ private function convertAugmentationValuesToIds($values)
219225
return "{$taxonomy}::{$value}";
220226
} else {
221227
if (! Str::contains($value, '::')) {
222-
throw new \Exception("Ambigious taxonomy term value [$value]. Field [{$this->field->handle()}] is configured with multiple taxonomies.");
228+
throw new Exception("Ambigious taxonomy term value [$value]. Field [{$this->field->handle()}] is configured with multiple taxonomies.");
223229
}
224230

225231
return $value;
@@ -599,7 +605,7 @@ public function relationshipQueryBuilder()
599605
->when($taxonomies, fn ($query) => $query->whereIn('taxonomy', $taxonomies));
600606
}
601607

602-
public function relationshipQueryIdMapFn(): ?\Closure
608+
public function relationshipQueryIdMapFn(): ?Closure
603609
{
604610
return $this->usingSingleTaxonomy()
605611
? fn ($term) => Str::after($term->id(), '::')

‎tests/Fieldtypes/TermsTest.php‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Facades\Tests\Factories\EntryFactory;
66
use Illuminate\Support\Collection;
7+
use Mockery;
78
use PHPUnit\Framework\Attributes\DataProvider;
89
use PHPUnit\Framework\Attributes\Test;
910
use Statamic\Contracts\Entries\Collection as CollectionContract;
@@ -93,6 +94,19 @@ public function it_augments_to_a_single_term_when_max_items_is_one()
9394
$this->assertEquals('tags::one', $augmented->id());
9495
}
9596

97+
#[Test]
98+
public function it_augments_to_the_term_of_the_configured_term_when_the_slug_exists_in_multiple_taxonomies()
99+
{
100+
Term::make('shared')->taxonomy('tags')->dataForLocale('en', ['title' => 'Shared Tag'])->save();
101+
Term::make('shared')->taxonomy('categories')->dataForLocale('en', ['title' => 'Shared Category'])->save();
102+
103+
$tag = $this->fieldtype(['taxonomies' => 'tags', 'max_items' => 1])->augment('shared');
104+
$category = $this->fieldtype(['taxonomies' => 'categories', 'max_items' => 1])->augment('shared');
105+
106+
$this->assertEquals('tags::shared', $tag->id());
107+
$this->assertEquals('categories::shared', $category->id());
108+
}
109+
96110
#[Test]
97111
public function it_localizes_the_augmented_items_to_the_parent_entrys_locale()
98112
{
@@ -396,7 +410,7 @@ public function it_falls_back_to_a_collection_where_clause_when_the_query_builde
396410
{
397411
$parent = EntryFactory::id('parent')->collection('blog')->slug('theparent')->locale('fr')->create();
398412

399-
$builder = \Mockery::mock(Builder::class);
413+
$builder = Mockery::mock(Builder::class);
400414
$builder->shouldReceive('whereIn')->with('id', ['tags::one'])->once()->andReturnSelf();
401415
$builder->shouldReceive('where')->with('site', 'fr')->once()->andReturnSelf();
402416
$builder->shouldReceive('where')->with('collection', 'blog')->once()->andReturnSelf();

0 commit comments

Comments
 (0)