diff --git a/DOCUMENTATION.md b/DOCUMENTATION.md index c47632a..e10d822 100644 --- a/DOCUMENTATION.md +++ b/DOCUMENTATION.md @@ -32,6 +32,7 @@ Before importing, you will need to do some preparation: 3. You can then map fields from your blueprint to fields/columns in your file. * Depending on the fieldtype, some fields may have additional options, like "Related Key" or "Create when missing". You can read more about these below. * Mapping is disabled for some fieldtypes, like the [Replicator fieldtype](https://statamic.dev/fieldtypes/replicator#content). If you wish to import these fields, you will need to build a [custom transformer](#transformers). + * When you're importing taxonomy terms into a non-default site (eg. not the first site) and the slugs differ between sites, you can use the "Default Slug" field to specify the slug of the term in the default site. 4. If you're importing entries, you will also need to specify a "Unique Field". This field will be used to determine if an entry already exists in Statamic. 5. Then, run the import and watch the magic happen! ✨ diff --git a/src/Imports/Import.php b/src/Imports/Import.php index 3cf9cfe..2b0b7fc 100644 --- a/src/Imports/Import.php +++ b/src/Imports/Import.php @@ -171,6 +171,17 @@ public function mappingFields(): Fields ]); } + if ($this->get('destination.type') === 'terms') { + $taxonomy = Taxonomy::find($this->get('destination.taxonomy')); + + if ($this->get('destination.site') !== $taxonomy->sites()->first()) { + $blueprint->ensureField('default_slug', [ + 'type' => 'slug', + 'display' => __('Default Slug'), + ]); + } + } + return $blueprint->fields(); } } diff --git a/src/Jobs/ImportItemJob.php b/src/Jobs/ImportItemJob.php index a69549e..9e5615d 100644 --- a/src/Jobs/ImportItemJob.php +++ b/src/Jobs/ImportItemJob.php @@ -13,6 +13,7 @@ use Statamic\Facades\Collection; use Statamic\Facades\Entry; use Statamic\Facades\Site; +use Statamic\Facades\Taxonomy; use Statamic\Facades\Term; use Statamic\Facades\User; use Statamic\Importer\Importer; @@ -120,8 +121,9 @@ protected function findOrCreateTerm(array $data): void { $term = Term::query() ->where('taxonomy', $this->import->get('destination.taxonomy')) - ->where('slug', $data['slug']) - ->first(); + ->where('id', $this->import->get('destination.taxonomy') . '::' . Arr::get($data, 'default_slug', $data['slug'])) + ->first() + ?->term(); if (! $term) { if (! in_array('create', $this->import->get('strategy'))) { @@ -133,14 +135,31 @@ protected function findOrCreateTerm(array $data): void ->blueprint($this->import->get('destination.blueprint')); } - if (Term::find($term->id()) && ! in_array('update', $this->import->get('strategy'))) { + if ( + Term::find($term->id())?->in($this->import->get('destination.site') ?? Site::default()->handle()) + && ! in_array('update', $this->import->get('strategy')) + ) { return; } + $term = $term->in($this->import->get('destination.site') ?? Site::default()->handle()); + $term->slug(Arr::pull($data, 'slug')); $term->merge($data); + $site = $this->import->get('destination.site') ?? Site::default()->handle(); + $defaultSite = Taxonomy::find($this->import->get('destination.taxonomy'))->sites()->first(); + + // If the term is *not* being created in the default site, we'll copy all the + // appropriate values into the default localization since it needs to exist. + if (! Term::find($term->id()) && $site !== $defaultSite) { + $term + ->in($defaultSite) + ->data($data) + ->slug($data['default_slug'] ?? $term->slug()); + } + $term->save(); }