Skip to content

Commit

Permalink
Multi-site taxonomy support
Browse files Browse the repository at this point in the history
  • Loading branch information
duncanmcclean committed Dec 19, 2024
1 parent 006cfd6 commit 7a83e87
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
1 change: 1 addition & 0 deletions DOCUMENTATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -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! ✨

Expand Down
11 changes: 11 additions & 0 deletions src/Imports/Import.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
25 changes: 22 additions & 3 deletions src/Jobs/ImportItemJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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'))) {
Expand All @@ -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();
}

Expand Down

0 comments on commit 7a83e87

Please sign in to comment.