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
12 changes: 9 additions & 3 deletions src/Fieldtypes/Terms.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Statamic\Fieldtypes;

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

if ($single && Blink::has($key = 'terms-augment-'.$site.'-'.json_encode($values))) {
// Use the taxonomy-qualified IDs in the cache key, since a value stored by a field
// limited to a single taxonomy will not contain the taxonomy handle on its own.
$key = 'terms-augment-'.$site.'-'.json_encode($this->convertAugmentationValuesToIds($values));

if ($single && Blink::has($key)) {
return Blink::get($key);
}

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

return $value;
Expand Down Expand Up @@ -599,7 +605,7 @@ public function relationshipQueryBuilder()
->when($taxonomies, fn ($query) => $query->whereIn('taxonomy', $taxonomies));
}

public function relationshipQueryIdMapFn(): ?\Closure
public function relationshipQueryIdMapFn(): ?Closure
{
return $this->usingSingleTaxonomy()
? fn ($term) => Str::after($term->id(), '::')
Expand Down
16 changes: 15 additions & 1 deletion tests/Fieldtypes/TermsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Facades\Tests\Factories\EntryFactory;
use Illuminate\Support\Collection;
use Mockery;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Test;
use Statamic\Contracts\Entries\Collection as CollectionContract;
Expand Down Expand Up @@ -93,6 +94,19 @@ public function it_augments_to_a_single_term_when_max_items_is_one()
$this->assertEquals('tags::one', $augmented->id());
}

#[Test]
public function it_augments_to_the_term_of_the_configured_term_when_the_slug_exists_in_multiple_taxonomies()
{
Term::make('shared')->taxonomy('tags')->dataForLocale('en', ['title' => 'Shared Tag'])->save();
Term::make('shared')->taxonomy('categories')->dataForLocale('en', ['title' => 'Shared Category'])->save();

$tag = $this->fieldtype(['taxonomies' => 'tags', 'max_items' => 1])->augment('shared');
$category = $this->fieldtype(['taxonomies' => 'categories', 'max_items' => 1])->augment('shared');

$this->assertEquals('tags::shared', $tag->id());
$this->assertEquals('categories::shared', $category->id());
}

#[Test]
public function it_localizes_the_augmented_items_to_the_parent_entrys_locale()
{
Expand Down Expand Up @@ -396,7 +410,7 @@ public function it_falls_back_to_a_collection_where_clause_when_the_query_builde
{
$parent = EntryFactory::id('parent')->collection('blog')->slug('theparent')->locale('fr')->create();

$builder = \Mockery::mock(Builder::class);
$builder = Mockery::mock(Builder::class);
$builder->shouldReceive('whereIn')->with('id', ['tags::one'])->once()->andReturnSelf();
$builder->shouldReceive('where')->with('site', 'fr')->once()->andReturnSelf();
$builder->shouldReceive('where')->with('collection', 'blog')->once()->andReturnSelf();
Expand Down
Loading