Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion src/Fieldtypes/Link.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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) {
Expand Down
40 changes: 40 additions & 0 deletions tests/Fieldtypes/LinkTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()
{
Expand Down
Loading