Skip to content

Commit

Permalink
Fix taxonomy terms not being imported correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
duncanmcclean committed Jan 14, 2025
1 parent 3e5e21c commit 0f7ff14
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/Transformers/TermsTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ public function transform(string $value): null|string|array

$term->save();

return $term->id();
return count($this->field->get('taxonomies')) > 1 ? $term->id() : $term->slug();
}

return $term?->id();
return count($this->field->get('taxonomies')) > 1 ? $term?->id() : $term?->slug();
})->filter();

return $this->field->get('max_items') === 1 ? $terms->first() : $terms->all();
Expand Down
28 changes: 27 additions & 1 deletion tests/Transformers/TermsTransformerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Statamic\Importer\Tests\Transformers;

use PHPUnit\Framework\Attributes\Test;
use Statamic\Facades\Blueprint;
use Statamic\Facades\Collection;
use Statamic\Facades\Taxonomy;
use Statamic\Facades\Term;
Expand Down Expand Up @@ -52,7 +53,32 @@ public function it_finds_existing_terms()

$output = $transformer->transform('Category One|Category Two|Category Three');

$this->assertEquals(['categories::one', 'categories::two', 'categories::three'], $output);
$this->assertEquals(['one', 'two', 'three'], $output);
}

#[Test]
public function it_finds_existing_terms_across_multiple_taxonomies()
{
Taxonomy::make('tags')->sites(['default'])->save();

Term::make()->taxonomy('categories')->slug('one')->set('title', 'Category One')->save();
Term::make()->taxonomy('categories')->slug('two')->set('title', 'Category Two')->save();
Term::make()->taxonomy('categories')->slug('three')->set('title', 'Category Three')->save();
Term::make()->taxonomy('tags')->slug('foo')->set('title', 'Foo')->save();

$blueprint = Blueprint::find($this->blueprint->fullyQualifiedHandle());
$blueprint->ensureField('stuff', ['type' => 'terms', 'taxonomies' => ['categories', 'tags']])->save();

$transformer = new TermsTransformer(
import: $this->import,
blueprint: $blueprint,
field: $blueprint->field('stuff'),
config: ['related_field' => 'title']
);

$output = $transformer->transform('Category One|Category Two|Category Three|Foo');

$this->assertEquals(['categories::one', 'categories::two', 'categories::three', 'tags::foo'], $output);
}

#[Test]
Expand Down

0 comments on commit 0f7ff14

Please sign in to comment.