From 5f8f481ec7c1b16689f3ff8045ef89ebbd7af785 Mon Sep 17 00:00:00 2001 From: Ryan Mitchell Date: Fri, 25 Aug 2023 14:13:34 +0100 Subject: [PATCH 01/14] Add routes control to taxonomies --- .../CP/Taxonomies/TaxonomiesController.php | 11 +++++++- .../Repositories/TaxonomyRepository.php | 12 ++++++--- src/Stache/Repositories/TermRepository.php | 12 ++++++--- src/Stache/Stores/TaxonomiesStore.php | 1 + src/Taxonomies/LocalizedTerm.php | 2 +- src/Taxonomies/Taxonomy.php | 27 ++++++++++++++++--- 6 files changed, 54 insertions(+), 11 deletions(-) diff --git a/src/Http/Controllers/CP/Taxonomies/TaxonomiesController.php b/src/Http/Controllers/CP/Taxonomies/TaxonomiesController.php index 03fe41b8e0..07bd408854 100644 --- a/src/Http/Controllers/CP/Taxonomies/TaxonomiesController.php +++ b/src/Http/Controllers/CP/Taxonomies/TaxonomiesController.php @@ -133,6 +133,9 @@ public function edit($taxonomy) 'collections' => $taxonomy->collections()->map->handle()->all(), 'sites' => $taxonomy->sites()->all(), 'preview_targets' => $taxonomy->basePreviewTargets(), + 'routes' => $taxonomy->routes()->unique()->count() === 1 + ? $taxonomy->routes()->first() + : $taxonomy->routes()->all(), ]; $fields = ($blueprint = $this->editFormBlueprint($taxonomy)) @@ -162,7 +165,8 @@ public function update(Request $request, $taxonomy) $taxonomy ->title($values['title']) - ->previewTargets($values['preview_targets']); + ->previewTargets($values['preview_targets']) + ->routes($values['routes']); if ($sites = array_get($values, 'sites')) { $taxonomy->sites($sites); @@ -276,6 +280,11 @@ protected function editFormBlueprint($taxonomy) 'routing' => [ 'display' => __('Routing & URLs'), 'fields' => [ + 'routes' => [ + 'display' => __('Route'), + 'instructions' => __('statamic::messages.collections_route_instructions'), + 'type' => 'collection_routes', + ], 'preview_targets' => [ 'display' => __('Preview Targets'), 'instructions' => __('statamic::messages.taxonomies_preview_targets_instructions'), diff --git a/src/Stache/Repositories/TaxonomyRepository.php b/src/Stache/Repositories/TaxonomyRepository.php index 2ac3c7a613..5ff153f2c4 100644 --- a/src/Stache/Repositories/TaxonomyRepository.php +++ b/src/Stache/Repositories/TaxonomyRepository.php @@ -78,7 +78,7 @@ public function findByUri(string $uri, string $site = null): ?Taxonomy // the slash trimmed off at this point. We'll make sure it's there. $uri = Str::ensureLeft($uri, '/'); - if (! $key = $this->findTaxonomyHandleByUri($uri)) { + if (! $key = $this->findTaxonomyHandleByUri($uri, $site)) { return null; } @@ -92,9 +92,15 @@ public static function bindings(): array ]; } - private function findTaxonomyHandleByUri($uri) + private function findTaxonomyHandleByUri($uri, $site) { - return $this->store->index('uri')->items()->flip()->get($uri); + $routes = $this->store->index('routes')->items()->map(fn($item) => $item->get($site))->flip(); + + if ($handle = $routes->get($uri)) { + return $handle; + } + + return $routes->get(Str::removeLeft($uri, '/')); } public function addPreviewTargets($handle, $targets) diff --git a/src/Stache/Repositories/TermRepository.php b/src/Stache/Repositories/TermRepository.php index affc765b75..3e790d23bc 100644 --- a/src/Stache/Repositories/TermRepository.php +++ b/src/Stache/Repositories/TermRepository.php @@ -74,7 +74,7 @@ public function findByUri(string $uri, string $site = null): ?Term return null; } - if (! $taxonomy = $this->findTaxonomyHandleByUri($taxonomy)) { + if (! $taxonomy = $this->findTaxonomyHandleByUri($taxonomy, $site)) { return null; } @@ -149,9 +149,15 @@ public static function bindings(): array ]; } - private function findTaxonomyHandleByUri($uri) + private function findTaxonomyHandleByUri($uri, $site) { - return $this->stache->store('taxonomies')->index('uri')->items()->flip()->get(Str::ensureLeft($uri, '/')); + $routes = $this->stache->store('taxonomies')->index('routes')->items()->map(fn($item) => $item->get($site))->flip(); + + if ($handle = $routes->get($uri)) { + return $handle; + } + + return $routes->get(Str::removeLeft($uri, '/')); } public function substitute($item) diff --git a/src/Stache/Stores/TaxonomiesStore.php b/src/Stache/Stores/TaxonomiesStore.php index 4a79a8949b..02d96a9a0d 100644 --- a/src/Stache/Stores/TaxonomiesStore.php +++ b/src/Stache/Stores/TaxonomiesStore.php @@ -43,6 +43,7 @@ public function makeItemFromFile($path, $contents) ->cascade(array_get($data, 'inject', [])) ->revisionsEnabled(array_get($data, 'revisions', false)) ->searchIndex(array_get($data, 'search_index')) + ->routes(array_get($data, 'route')) ->defaultPublishState($this->getDefaultPublishState($data)) ->sites($sites) ->previewTargets($this->normalizePreviewTargets(array_get($data, 'preview_targets', []))); diff --git a/src/Taxonomies/LocalizedTerm.php b/src/Taxonomies/LocalizedTerm.php index 57cdc57922..d6a3411b69 100644 --- a/src/Taxonomies/LocalizedTerm.php +++ b/src/Taxonomies/LocalizedTerm.php @@ -337,7 +337,7 @@ public function apiUrl() public function route() { - $route = '/'.str_replace('_', '-', $this->taxonomyHandle()).'/{slug}'; + $route = '/'.$this->taxonomy()->routes()->get($this->locale()).'/{slug}'; if ($this->collection()) { $collectionUrl = $this->collection()->uri($this->locale()) ?? $this->collection()->handle(); diff --git a/src/Taxonomies/Taxonomy.php b/src/Taxonomies/Taxonomy.php index be01dd7330..ba79a1ad63 100644 --- a/src/Taxonomies/Taxonomy.php +++ b/src/Taxonomies/Taxonomy.php @@ -36,6 +36,7 @@ class Taxonomy implements Contract, Responsable, AugmentableContract, ArrayAcces protected $handle; protected $title; protected $blueprints = []; + protected $routes = []; protected $sites = []; protected $collection; protected $defaultPublishState = true; @@ -249,6 +250,7 @@ public function fileData() 'title' => $this->title, 'blueprints' => $this->blueprints, 'preview_targets' => $this->previewTargetsForFile(), + 'route' => $this->routes, ]; if (Site::hasMultiple()) { @@ -310,13 +312,13 @@ public function absoluteUrl() return URL::tidy(Site::current()->absoluteUrl().$this->uri()); } - public function uri() + public function uri($site = null) { - $site = Site::current(); + $site = $site ?? Site::current(); $prefix = $this->collection() ? $this->collection()->uri($site->handle()) : '/'; - return URL::tidy($prefix.str_replace('_', '-', '/'.$this->handle)); + return URL::tidy($prefix.$this->routes()->get($site->handle())); } public function collection($collection = null) @@ -334,6 +336,25 @@ public function collections() })->values(); } + public function routes($routes = null) + { + return $this + ->fluentlyGetOrSet('routes') + ->getter(function ($routes) { + return $this->sites()->mapWithKeys(function ($site) use ($routes) { + $siteRoute = is_string($routes) ? $routes : ($routes[$site] ?? str_replace('_', '-', '/'.$this->handle)); + + return [$site => $siteRoute]; + }); + }) + ->args(func_get_args()); + } + + public function route($site) + { + return $this->routes()->get($site); + } + public function toResponse($request) { if (! view()->exists($this->template())) { From 590761f07be3e7c978c2f5d82173887f9f475c8b Mon Sep 17 00:00:00 2001 From: Ryan Mitchell Date: Fri, 25 Aug 2023 14:20:11 +0100 Subject: [PATCH 02/14] Add ensureLeft as term repo does it the reverse to taxonomy repo --- src/Stache/Repositories/TermRepository.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Stache/Repositories/TermRepository.php b/src/Stache/Repositories/TermRepository.php index 3e790d23bc..98e65336f7 100644 --- a/src/Stache/Repositories/TermRepository.php +++ b/src/Stache/Repositories/TermRepository.php @@ -157,7 +157,7 @@ private function findTaxonomyHandleByUri($uri, $site) return $handle; } - return $routes->get(Str::removeLeft($uri, '/')); + return $routes->get(Str::ensureLeft($uri, '/')); } public function substitute($item) From 1039ccbaa86575f3b275e0a4bdf1a898f64190ef Mon Sep 17 00:00:00 2001 From: Ryan Mitchell Date: Fri, 25 Aug 2023 14:21:21 +0100 Subject: [PATCH 03/14] :beer: --- src/Stache/Repositories/TaxonomyRepository.php | 2 +- src/Stache/Repositories/TermRepository.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Stache/Repositories/TaxonomyRepository.php b/src/Stache/Repositories/TaxonomyRepository.php index 5ff153f2c4..ced5fe71e5 100644 --- a/src/Stache/Repositories/TaxonomyRepository.php +++ b/src/Stache/Repositories/TaxonomyRepository.php @@ -94,7 +94,7 @@ public static function bindings(): array private function findTaxonomyHandleByUri($uri, $site) { - $routes = $this->store->index('routes')->items()->map(fn($item) => $item->get($site))->flip(); + $routes = $this->store->index('routes')->items()->map(fn ($item) => $item->get($site))->flip(); if ($handle = $routes->get($uri)) { return $handle; diff --git a/src/Stache/Repositories/TermRepository.php b/src/Stache/Repositories/TermRepository.php index 98e65336f7..b61999be9f 100644 --- a/src/Stache/Repositories/TermRepository.php +++ b/src/Stache/Repositories/TermRepository.php @@ -151,7 +151,7 @@ public static function bindings(): array private function findTaxonomyHandleByUri($uri, $site) { - $routes = $this->stache->store('taxonomies')->index('routes')->items()->map(fn($item) => $item->get($site))->flip(); + $routes = $this->stache->store('taxonomies')->index('routes')->items()->map(fn ($item) => $item->get($site))->flip(); if ($handle = $routes->get($uri)) { return $handle; From b8d565fa304fea9f3df757d8d82758ac6f144254 Mon Sep 17 00:00:00 2001 From: Ryan Mitchell Date: Fri, 25 Aug 2023 14:24:58 +0100 Subject: [PATCH 04/14] Didn't need this change after all, so revert it --- src/Taxonomies/Taxonomy.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Taxonomies/Taxonomy.php b/src/Taxonomies/Taxonomy.php index ba79a1ad63..9bc7f4c28b 100644 --- a/src/Taxonomies/Taxonomy.php +++ b/src/Taxonomies/Taxonomy.php @@ -312,9 +312,9 @@ public function absoluteUrl() return URL::tidy(Site::current()->absoluteUrl().$this->uri()); } - public function uri($site = null) + public function uri() { - $site = $site ?? Site::current(); + $site = Site::current(); $prefix = $this->collection() ? $this->collection()->uri($site->handle()) : '/'; From f1f46ae9ef73bb03515779cc7a2acf038ee92b69 Mon Sep 17 00:00:00 2001 From: Ryan Mitchell Date: Fri, 25 Aug 2023 21:58:11 +0100 Subject: [PATCH 05/14] Fix tests --- src/Stache/Repositories/TaxonomyRepository.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/Stache/Repositories/TaxonomyRepository.php b/src/Stache/Repositories/TaxonomyRepository.php index ced5fe71e5..094040a274 100644 --- a/src/Stache/Repositories/TaxonomyRepository.php +++ b/src/Stache/Repositories/TaxonomyRepository.php @@ -11,11 +11,13 @@ class TaxonomyRepository implements RepositoryContract { + protected $stache; protected $store; protected $additionalPreviewTargets = []; public function __construct(Stache $stache) { + $this->stache = $stache; $this->store = $stache->store('taxonomies'); } @@ -94,6 +96,8 @@ public static function bindings(): array private function findTaxonomyHandleByUri($uri, $site) { + $site = $site ?? $this->stache->sites()->first(); + $routes = $this->store->index('routes')->items()->map(fn ($item) => $item->get($site))->flip(); if ($handle = $routes->get($uri)) { From c0aed0a38579e8c8dcfabf31e01ac40b234b92f9 Mon Sep 17 00:00:00 2001 From: Ryan Mitchell Date: Wed, 18 Oct 2023 21:46:31 +0100 Subject: [PATCH 06/14] Add taxonomy specific field instructions --- resources/lang/en/messages.php | 1 + src/Http/Controllers/CP/Taxonomies/TaxonomiesController.php | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/resources/lang/en/messages.php b/resources/lang/en/messages.php index 6d7ed614ba..fb59738ffd 100644 --- a/resources/lang/en/messages.php +++ b/resources/lang/en/messages.php @@ -198,6 +198,7 @@ 'taxonomies_collections_instructions' => 'The collections that use this taxonomy.', 'taxonomies_preview_targets_instructions' => 'The URLs to be viewable within Live Preview. Learn more in the [documentation](https://statamic.dev/live-preview#preview-targets).', 'taxonomies_preview_target_refresh_instructions' => 'Automatically refresh the preview while editing. Disabling this will use postMessage.', + 'taxonomies_route_instructions' => 'The route controls show and index URL patterns. Learn more in the [documentation](https://statamic.dev/taxonomies#routing).', 'taxonomy_configure_handle_instructions' => 'Used to reference this taxonomy on the frontend. It\'s non-trivial to change later.', 'taxonomy_configure_intro' => 'A taxonomy is a system of classifying data around a set of unique characteristics, such as categories, tags, or colors.', 'taxonomy_configure_title_instructions' => 'We recommend using a plural noun, like "Categories" or "Tags".', diff --git a/src/Http/Controllers/CP/Taxonomies/TaxonomiesController.php b/src/Http/Controllers/CP/Taxonomies/TaxonomiesController.php index 07bd408854..a9b13221a4 100644 --- a/src/Http/Controllers/CP/Taxonomies/TaxonomiesController.php +++ b/src/Http/Controllers/CP/Taxonomies/TaxonomiesController.php @@ -282,7 +282,7 @@ protected function editFormBlueprint($taxonomy) 'fields' => [ 'routes' => [ 'display' => __('Route'), - 'instructions' => __('statamic::messages.collections_route_instructions'), + 'instructions' => __('statamic::messages.taxonomies_route_instructions'), 'type' => 'collection_routes', ], 'preview_targets' => [ From f86fc3d1874bde6ce605357e90319ac2d707fa39 Mon Sep 17 00:00:00 2001 From: Ryan Mitchell Date: Thu, 19 Oct 2023 07:35:45 +0100 Subject: [PATCH 07/14] Add some tests --- tests/Data/Taxonomies/TaxonomyTest.php | 66 ++++++++++++++++++++++++++ tests/Data/Taxonomies/TermTest.php | 34 +++++++++++++ 2 files changed, 100 insertions(+) diff --git a/tests/Data/Taxonomies/TaxonomyTest.php b/tests/Data/Taxonomies/TaxonomyTest.php index 2502ae16e6..1a13afbb99 100644 --- a/tests/Data/Taxonomies/TaxonomyTest.php +++ b/tests/Data/Taxonomies/TaxonomyTest.php @@ -364,6 +364,72 @@ public function if_saving_event_returns_false_the_taxonomy_doesnt_save() Event::assertNotDispatched(TaxonomySaved::class); } + /** @test */ + public function it_gets_and_sets_the_routes() + { + Site::setConfig(['sites' => [ + 'en' => ['url' => 'http://domain.com/'], + 'fr' => ['url' => 'http://domain.com/fr/'], + 'de' => ['url' => 'http://domain.com/de/'], + ]]); + + // A taxonomy with no sites uses the default site. + $taxonomy = new Taxonomy; + $this->assertInstanceOf(\Illuminate\Support\Collection::class, $taxonomy->routes()); + $this->assertEquals(['en' => '/'], $taxonomy->routes()->all()); + + $return = $taxonomy->routes([ + 'en' => 'blog/', + 'fr' => 'le-blog/', + 'de' => 'das-blog/', + ]); + + $this->assertEquals($taxonomy, $return); + $this->assertInstanceOf(\Illuminate\Support\Collection::class, $taxonomy->routes()); + + // Only routes corresponding to the collection's sites will be returned. + $this->assertEquals(['en' => 'blog/'], $taxonomy->routes()->all()); + $this->assertEquals('blog/', $taxonomy->route('en')); + $this->assertNull($taxonomy->route('fr')); + $this->assertNull($taxonomy->route('de')); + $this->assertNull($taxonomy->route('unknown')); + + $taxonomy->sites(['en', 'fr']); + + $this->assertEquals([ + 'en' => 'blog/', + 'fr' => 'le-blog/', + ], $taxonomy->routes()->all()); + $this->assertEquals('blog/', $taxonomy->route('en')); + $this->assertEquals('le-blog/', $taxonomy->route('fr')); + $this->assertNull($taxonomy->route('de')); + $this->assertNull($taxonomy->route('unknown')); + } + + /** @test */ + public function it_sets_all_the_routes_identically() + { + Site::setConfig(['sites' => [ + 'en' => ['url' => 'http://domain.com/'], + 'fr' => ['url' => 'http://domain.com/fr/'], + 'de' => ['url' => 'http://domain.com/de/'], + ]]); + + $taxonomy = (new Taxonomy)->sites(['en', 'fr']); + + $return = $taxonomy->routes('{slug}'); + + $this->assertEquals($taxonomy, $return); + $this->assertEquals([ + 'en' => '{slug}', + 'fr' => '{slug}', + ], $taxonomy->routes()->all()); + $this->assertEquals('{slug}', $taxonomy->route('en')); + $this->assertEquals('{slug}', $taxonomy->route('fr')); + $this->assertNull($taxonomy->route('de')); + $this->assertNull($taxonomy->route('unknown')); + } + public function additionalPreviewTargetProvider() { return [ diff --git a/tests/Data/Taxonomies/TermTest.php b/tests/Data/Taxonomies/TermTest.php index f54331f40f..940d854593 100644 --- a/tests/Data/Taxonomies/TermTest.php +++ b/tests/Data/Taxonomies/TermTest.php @@ -288,4 +288,38 @@ public function it_gets_preview_targets() ['label' => 'Show', 'format' => 'http://preview.com/{locale}/tags/{slug}?preview=true', 'url' => 'http://preview.com/de/tags/das-foo?preview=true'], ], $termDe->previewTargets()->all()); } + + /** @test */ + public function it_gets_routes() + { + Facades\Site::setConfig(['default' => 'en', 'sites' => [ + 'en' => ['url' => 'http://domain.com/'], + 'fr' => ['url' => 'http://domain.com/fr/'], + 'de' => ['url' => 'http://domain.de/'], + ]]); + + $taxonomy = tap(Taxonomy::make('tags')->sites(['en', 'fr', 'de'])->routes('tags'))->save(); + + $term = (new Term)->taxonomy('tags'); + + $termEn = $term->in('en')->slug('foo'); + $termFr = $term->in('fr')->slug('le-foo'); + $termDe = $term->in('de')->slug('das-foo'); + + $this->assertEquals('/tags/{slug}', $termEn->route()); + $this->assertEquals('/tags/{slug}', $termFr->route()); + $this->assertEquals('/tags/{slug}', $termDe->route()); + + $taxonomy->routes([ + 'en' => 'blog', + 'fr' => 'le-blog', + 'de' => 'das-blog', + ]); + + $taxonomy->save(); + + $this->assertEquals('/blog/{slug}', $termEn->route()); + $this->assertEquals('/le-blog/{slug}', $termFr->route()); + $this->assertEquals('/das-blog/{slug}', $termDe->route()); + } } From 28eaa33733c6026c93ff19d3ee9cb37a82457c09 Mon Sep 17 00:00:00 2001 From: Ryan Mitchell Date: Tue, 24 Oct 2023 22:06:07 +0100 Subject: [PATCH 08/14] Fix paths that have more than 1 slash --- src/Stache/Repositories/TaxonomyRepository.php | 2 +- src/Stache/Repositories/TermRepository.php | 7 +++++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/Stache/Repositories/TaxonomyRepository.php b/src/Stache/Repositories/TaxonomyRepository.php index 094040a274..38c75094af 100644 --- a/src/Stache/Repositories/TaxonomyRepository.php +++ b/src/Stache/Repositories/TaxonomyRepository.php @@ -98,7 +98,7 @@ private function findTaxonomyHandleByUri($uri, $site) { $site = $site ?? $this->stache->sites()->first(); - $routes = $this->store->index('routes')->items()->map(fn ($item) => $item->get($site))->flip(); + $routes = $this->store->index('routes')->items()->map(fn ($item) => $item->get($site))->filter()->flip(); if ($handle = $routes->get($uri)) { return $handle; diff --git a/src/Stache/Repositories/TermRepository.php b/src/Stache/Repositories/TermRepository.php index 27108dc78a..845a9c6e06 100644 --- a/src/Stache/Repositories/TermRepository.php +++ b/src/Stache/Repositories/TermRepository.php @@ -68,7 +68,10 @@ public function findByUri(string $uri, string $site = null): ?Term $uri = Str::removeLeft($uri, '/'); - [$taxonomy, $slug] = array_pad(explode('/', $uri), 2, null); + $uriParts = array_pad(explode('/', $uri), 2, null); + + $slug = array_pop($uriParts); + $taxonomy = implode('/', $uriParts); if (! $slug) { return null; @@ -151,7 +154,7 @@ public static function bindings(): array private function findTaxonomyHandleByUri($uri, $site) { - $routes = $this->stache->store('taxonomies')->index('routes')->items()->map(fn ($item) => $item->get($site))->flip(); + $routes = $this->stache->store('taxonomies')->index('routes')->items()->map(fn ($item) => $item->get($site))->filter()->flip(); if ($handle = $routes->get($uri)) { return $handle; From 69691a68ca26cef046470b5ee9d6f74357d1b393 Mon Sep 17 00:00:00 2001 From: Ryan Mitchell Date: Tue, 24 Oct 2023 22:06:31 +0100 Subject: [PATCH 09/14] Allow Antlers parsing (?) --- src/Taxonomies/LocalizedTerm.php | 2 +- tests/Data/Taxonomies/TermTest.php | 12 ++++++------ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/Taxonomies/LocalizedTerm.php b/src/Taxonomies/LocalizedTerm.php index 3cf9dafc05..a19801fd03 100644 --- a/src/Taxonomies/LocalizedTerm.php +++ b/src/Taxonomies/LocalizedTerm.php @@ -337,7 +337,7 @@ public function apiUrl() public function route() { - $route = '/'.$this->taxonomy()->routes()->get($this->locale()).'/{slug}'; + $route = '/'.$this->taxonomy()->routes()->get($this->locale()).'/{{ slug }}'; if ($this->collection()) { $collectionUrl = $this->collection()->uri($this->locale()) ?? $this->collection()->handle(); diff --git a/tests/Data/Taxonomies/TermTest.php b/tests/Data/Taxonomies/TermTest.php index 940d854593..ac4b2a8cc2 100644 --- a/tests/Data/Taxonomies/TermTest.php +++ b/tests/Data/Taxonomies/TermTest.php @@ -306,9 +306,9 @@ public function it_gets_routes() $termFr = $term->in('fr')->slug('le-foo'); $termDe = $term->in('de')->slug('das-foo'); - $this->assertEquals('/tags/{slug}', $termEn->route()); - $this->assertEquals('/tags/{slug}', $termFr->route()); - $this->assertEquals('/tags/{slug}', $termDe->route()); + $this->assertEquals('/tags/{{ slug }}', $termEn->route()); + $this->assertEquals('/tags/{{ slug }}', $termFr->route()); + $this->assertEquals('/tags/{{ slug }}', $termDe->route()); $taxonomy->routes([ 'en' => 'blog', @@ -318,8 +318,8 @@ public function it_gets_routes() $taxonomy->save(); - $this->assertEquals('/blog/{slug}', $termEn->route()); - $this->assertEquals('/le-blog/{slug}', $termFr->route()); - $this->assertEquals('/das-blog/{slug}', $termDe->route()); + $this->assertEquals('/blog/{{ slug }}', $termEn->route()); + $this->assertEquals('/le-blog/{{ slug }}', $termFr->route()); + $this->assertEquals('/das-blog/{{ slug }}', $termDe->route()); } } From a7bce332016ee988188a4d0b11901c79b40dad71 Mon Sep 17 00:00:00 2001 From: Ryan Mitchell Date: Wed, 25 Oct 2023 16:09:52 +0100 Subject: [PATCH 10/14] Seems this is how its done elsewhere so why not --- src/Taxonomies/LocalizedTerm.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Taxonomies/LocalizedTerm.php b/src/Taxonomies/LocalizedTerm.php index a19801fd03..a2c05b4012 100644 --- a/src/Taxonomies/LocalizedTerm.php +++ b/src/Taxonomies/LocalizedTerm.php @@ -337,7 +337,8 @@ public function apiUrl() public function route() { - $route = '/'.$this->taxonomy()->routes()->get($this->locale()).'/{{ slug }}'; + $route = '/'.$this->taxonomy()->routes()->get($this->locale()).'/'; + $route .= Str::contains($route, '{{') ? '{{ slug }}' : '{slug}'; if ($this->collection()) { $collectionUrl = $this->collection()->uri($this->locale()) ?? $this->collection()->handle(); From 7eb1c85c0e879bdb6c3d81362d6d6831f80e31d4 Mon Sep 17 00:00:00 2001 From: Ryan Mitchell Date: Wed, 25 Oct 2023 16:16:17 +0100 Subject: [PATCH 11/14] Revert test change --- tests/Data/Taxonomies/TermTest.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/Data/Taxonomies/TermTest.php b/tests/Data/Taxonomies/TermTest.php index ac4b2a8cc2..940d854593 100644 --- a/tests/Data/Taxonomies/TermTest.php +++ b/tests/Data/Taxonomies/TermTest.php @@ -306,9 +306,9 @@ public function it_gets_routes() $termFr = $term->in('fr')->slug('le-foo'); $termDe = $term->in('de')->slug('das-foo'); - $this->assertEquals('/tags/{{ slug }}', $termEn->route()); - $this->assertEquals('/tags/{{ slug }}', $termFr->route()); - $this->assertEquals('/tags/{{ slug }}', $termDe->route()); + $this->assertEquals('/tags/{slug}', $termEn->route()); + $this->assertEquals('/tags/{slug}', $termFr->route()); + $this->assertEquals('/tags/{slug}', $termDe->route()); $taxonomy->routes([ 'en' => 'blog', @@ -318,8 +318,8 @@ public function it_gets_routes() $taxonomy->save(); - $this->assertEquals('/blog/{{ slug }}', $termEn->route()); - $this->assertEquals('/le-blog/{{ slug }}', $termFr->route()); - $this->assertEquals('/das-blog/{{ slug }}', $termDe->route()); + $this->assertEquals('/blog/{slug}', $termEn->route()); + $this->assertEquals('/le-blog/{slug}', $termFr->route()); + $this->assertEquals('/das-blog/{slug}', $termDe->route()); } } From ac7d5a94f970d548f4f25aac02bc06423ea8dd98 Mon Sep 17 00:00:00 2001 From: Ryan Mitchell Date: Wed, 13 Dec 2023 12:36:48 +0000 Subject: [PATCH 12/14] :beer: --- src/Http/Controllers/CP/Taxonomies/TaxonomiesController.php | 2 +- tests/Data/Taxonomies/TaxonomyTest.php | 2 +- tests/Data/Taxonomies/TermTest.php | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Http/Controllers/CP/Taxonomies/TaxonomiesController.php b/src/Http/Controllers/CP/Taxonomies/TaxonomiesController.php index 16bdc28c2a..1dda7a1f29 100644 --- a/src/Http/Controllers/CP/Taxonomies/TaxonomiesController.php +++ b/src/Http/Controllers/CP/Taxonomies/TaxonomiesController.php @@ -169,7 +169,7 @@ public function update(Request $request, $taxonomy) $taxonomy ->title($values['title']) ->previewTargets($values['preview_targets']) - ->routes($values['routes']); + ->routes($values['routes']) ->termTemplate($values['term_template'] ?? null) ->template($values['template'] ?? null) ->layout($values['layout'] ?? null); diff --git a/tests/Data/Taxonomies/TaxonomyTest.php b/tests/Data/Taxonomies/TaxonomyTest.php index 4a1e026e24..5a53c41456 100644 --- a/tests/Data/Taxonomies/TaxonomyTest.php +++ b/tests/Data/Taxonomies/TaxonomyTest.php @@ -456,7 +456,7 @@ public function it_sets_all_the_routes_identically() $this->assertNull($taxonomy->route('de')); $this->assertNull($taxonomy->route('unknown')); } - + /** @test */ public function it_gets_and_sets_the_layout() { diff --git a/tests/Data/Taxonomies/TermTest.php b/tests/Data/Taxonomies/TermTest.php index 625e757f7c..2281cc8081 100644 --- a/tests/Data/Taxonomies/TermTest.php +++ b/tests/Data/Taxonomies/TermTest.php @@ -347,7 +347,7 @@ public function it_gets_routes() $this->assertEquals('/le-blog/{slug}', $termFr->route()); $this->assertEquals('/das-blog/{slug}', $termDe->route()); } - + /** @test */ public function it_gets_and_sets_the_layout() { From ac6e626fd6f976c0ac7ae0b76ee96c559faa6137 Mon Sep 17 00:00:00 2001 From: Ryan Mitchell Date: Fri, 10 May 2024 12:17:09 +0100 Subject: [PATCH 13/14] Fix tests --- tests/Data/Taxonomies/TaxonomyTest.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/Data/Taxonomies/TaxonomyTest.php b/tests/Data/Taxonomies/TaxonomyTest.php index 90ac8addce..77423818c8 100644 --- a/tests/Data/Taxonomies/TaxonomyTest.php +++ b/tests/Data/Taxonomies/TaxonomyTest.php @@ -394,11 +394,11 @@ public function if_saving_event_returns_false_the_taxonomy_doesnt_save() /** @test */ public function it_gets_and_sets_the_routes() { - Site::setConfig(['sites' => [ + $this->setSites([ 'en' => ['url' => 'http://domain.com/'], 'fr' => ['url' => 'http://domain.com/fr/'], 'de' => ['url' => 'http://domain.com/de/'], - ]]); + ]); // A taxonomy with no sites uses the default site. $taxonomy = new Taxonomy; @@ -436,11 +436,11 @@ public function it_gets_and_sets_the_routes() /** @test */ public function it_sets_all_the_routes_identically() { - Site::setConfig(['sites' => [ + $this->setSites([ 'en' => ['url' => 'http://domain.com/'], 'fr' => ['url' => 'http://domain.com/fr/'], 'de' => ['url' => 'http://domain.com/de/'], - ]]); + ]); $taxonomy = (new Taxonomy)->sites(['en', 'fr']); From be6d8998f8b113fd3a346dd9ebe8d5fc3362cca0 Mon Sep 17 00:00:00 2001 From: Ryan Mitchell Date: Fri, 10 May 2024 12:41:10 +0100 Subject: [PATCH 14/14] Fix test --- tests/Data/Taxonomies/TermTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Data/Taxonomies/TermTest.php b/tests/Data/Taxonomies/TermTest.php index 37b3f944b9..a7a251a45f 100644 --- a/tests/Data/Taxonomies/TermTest.php +++ b/tests/Data/Taxonomies/TermTest.php @@ -319,11 +319,11 @@ public function it_gets_preview_targets() /** @test */ public function it_gets_routes() { - Facades\Site::setConfig(['default' => 'en', 'sites' => [ + $this->setSites([ 'en' => ['url' => 'http://domain.com/'], 'fr' => ['url' => 'http://domain.com/fr/'], 'de' => ['url' => 'http://domain.de/'], - ]]); + ]); $taxonomy = tap(Taxonomy::make('tags')->sites(['en', 'fr', 'de'])->routes('tags'))->save();