From 68c545445c4e5aa3c5681081129e04f948e7b001 Mon Sep 17 00:00:00 2001 From: edalzell Date: Thu, 29 Oct 2026 14:07:06 -0700 Subject: [PATCH 1/2] Add link index tests for entries on another site --- tests/Fieldtypes/LinkTest.php | 40 +++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/tests/Fieldtypes/LinkTest.php b/tests/Fieldtypes/LinkTest.php index a2607cf04df..9acdcf1adda 100644 --- a/tests/Fieldtypes/LinkTest.php +++ b/tests/Fieldtypes/LinkTest.php @@ -6,6 +6,7 @@ use Mockery; use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\Attributes\Test; +use Statamic\Contracts\Data\Localization; use Statamic\Entries\Entry; use Statamic\Facades; use Statamic\Fields\ArrayableString; @@ -156,6 +157,45 @@ public function it_pre_processes_entry_reference_for_index() ); } + #[Test] + public function it_pre_processes_entry_on_another_site_with_absolute_url_for_index() + { + $entry = Mockery::mock(\Statamic\Contracts\Entries\Entry::class, Localization::class); + $entry->shouldReceive('locale')->andReturn('fr'); + $entry->shouldReceive('absoluteUrl')->once()->andReturn('http://example.fr/the-entry-url'); + $entry->shouldNotReceive('url'); + + Facades\Entry::shouldReceive('find')->with('entry-id')->once()->andReturn($entry); + + $fieldtype = (new Link)->setField(new Field('test', ['type' => 'link'])); + + $this->assertEquals( + ['type' => 'entry', 'url' => 'http://example.fr/the-entry-url', 'icon' => 'collections'], + $fieldtype->preProcessIndex('entry::entry-id') + ); + } + + #[Test] + public function it_pre_processes_entry_on_the_parents_site_with_relative_url_for_index() + { + $entry = Mockery::mock(\Statamic\Contracts\Entries\Entry::class, Localization::class); + $entry->shouldReceive('locale')->andReturn('fr'); + $entry->shouldReceive('url')->once()->andReturn('/the-entry-url'); + $entry->shouldNotReceive('absoluteUrl'); + + $parent = Mockery::mock(\Statamic\Contracts\Entries\Entry::class, Localization::class); + $parent->shouldReceive('locale')->andReturn('fr'); + + Facades\Entry::shouldReceive('find')->with('entry-id')->once()->andReturn($entry); + + $fieldtype = (new Link)->setField((new Field('test', ['type' => 'link']))->setParent($parent)); + + $this->assertEquals( + ['type' => 'entry', 'url' => '/the-entry-url', 'icon' => 'collections'], + $fieldtype->preProcessIndex('entry::entry-id') + ); + } + #[Test] public function it_pre_processes_asset_reference_for_index() { From 4f8affc99be4db39f8d1fa276168eee9b74996c4 Mon Sep 17 00:00:00 2001 From: edalzell Date: Thu, 29 Oct 2026 14:07:09 -0700 Subject: [PATCH 2/2] Use absolute URL in link index for entries on another site --- src/Fieldtypes/Link.php | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/src/Fieldtypes/Link.php b/src/Fieldtypes/Link.php index 65fb1c3a1aa..27ea4f5ebcd 100644 --- a/src/Fieldtypes/Link.php +++ b/src/Fieldtypes/Link.php @@ -3,9 +3,11 @@ namespace Statamic\Fieldtypes; use Facades\Statamic\Routing\ResolveRedirect; +use Statamic\Contracts\Data\Localization; use Statamic\Contracts\Entries\Collection; use Statamic\Contracts\Entries\Entry; use Statamic\Facades\GraphQL; +use Statamic\Facades\Site; use Statamic\Fields\Field; use Statamic\Fields\Fieldtype; use Statamic\Fieldtypes\Link\ArrayableLink; @@ -103,7 +105,7 @@ public function preProcessIndex($data) return null; } - if (! ($url = is_object($item) ? $item->url() : $item)) { + if (! ($url = $this->indexUrl($item))) { return null; } @@ -165,6 +167,26 @@ public function preload() ]; } + private function indexUrl($item) + { + if (! is_object($item)) { + return $item; + } + + if ($item instanceof Localization && $item->locale() !== $this->indexSite()) { + return $item->absoluteUrl(); + } + + return $item->url(); + } + + private function indexSite(): string + { + $parent = $this->field->parent(); + + return $parent instanceof Localization ? $parent->locale() : Site::selected()->handle(); + } + private function linkTypeFor($value): ?string { if (! $value) {