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
2 changes: 2 additions & 0 deletions resources/js/bootstrap/fieldtypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import HtmlFieldtype from '../components/fieldtypes/HtmlFieldtype.vue';
import IconFieldtype from '../components/fieldtypes/IconFieldtype.vue';
import IntegerFieldtype from '../components/fieldtypes/IntegerFieldtype.vue';
import LinkFieldtype from '../components/fieldtypes/LinkFieldtype.vue';
import LinkIndexFieldtype from '../components/fieldtypes/LinkIndexFieldtype.vue';
import ListFieldtype from '../components/fieldtypes/ListFieldtype.vue';
import ListIndexFieldtype from '../components/fieldtypes/ListIndexFieldtype.vue';
import MarkdownButtonsSettingFieldtype from '../components/fieldtypes/markdown/MarkdownButtonsSettingFieldtype.vue';
Expand Down Expand Up @@ -107,6 +108,7 @@ export default function registerFieldtypes(app) {
app.component('icon-fieldtype', IconFieldtype);
app.component('integer-fieldtype', IntegerFieldtype);
app.component('link-fieldtype', LinkFieldtype);
app.component('link-fieldtype-index', LinkIndexFieldtype);
app.component('list-fieldtype', ListFieldtype);
app.component('list-fieldtype-index', ListIndexFieldtype);
app.component(
Expand Down
16 changes: 16 additions & 0 deletions resources/js/components/fieldtypes/LinkIndexFieldtype.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<script setup>
import IndexFieldtype from '@/components/fieldtypes/index-fieldtype.js';
import { Icon } from '@ui';

const props = defineProps(IndexFieldtype.props);
</script>

<template>
<a v-if="value" :key="value.url" :href="value.url" target="_blank" rel="noopener noreferrer" class="flex items-center space-x-2 text-ellipsis">
<Icon v-if="value.type === 'asset'" name="assets" class="size-3 flex-shrink-0" />
<Icon v-else-if="value.type === 'entry'" name="collections" class="size-3 flex-shrink-0" />
<Icon v-else-if="value.type === 'child'" name="page" class="size-3 flex-shrink-0" />
<Icon v-else name="external-link" class="size-3 flex-shrink-0" />
<span v-text="value.url" />
</a>
</template>
28 changes: 28 additions & 0 deletions src/Fieldtypes/Link.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,34 @@ public function augment($value)
);
}

public function preProcessIndex($data)
{
if (! $data) {
return null;
}

if ($data === '@child' && ! $this->field->parent() instanceof Entry) {
return null;
}

if (! $item = ResolveRedirect::item($data, $this->field->parent())) {
return null;
}

if (! ($url = is_object($item) ? $item->url() : $item)) {
return null;
}

$type = match (true) {
$data === '@child' => 'child',
Str::startsWith($data, 'asset::') => 'asset',
Str::startsWith($data, 'entry::') => 'entry',
default => 'url',
};

return ['type' => $type, 'url' => $url];
}

public function preload()
{
$value = $this->field->value();
Expand Down
173 changes: 173 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\Test;
use Statamic\Entries\Entry;
use Statamic\Facades;
use Statamic\Fields\ArrayableString;
use Statamic\Fields\Field;
use Statamic\Fieldtypes\Link;
Expand Down Expand Up @@ -88,4 +89,176 @@ public function it_augments_null_to_null()
$this->assertNull($augmented->value());
$this->assertEquals(['url' => null], $augmented->toArray());
}

#[Test]
public function it_pre_processes_url_for_index()
{
$fieldtype = (new Link)->setField(new Field('test', ['type' => 'link']));

$this->assertEquals(
['type' => 'url', 'url' => 'https://example.com'],
$fieldtype->preProcessIndex('https://example.com')
);
}

#[Test]
public function it_pre_processes_numeric_value_for_index()
{
$fieldtype = (new Link)->setField(new Field('test', ['type' => 'link']));

$this->assertEquals(
['type' => 'url', 'url' => 404],
$fieldtype->preProcessIndex('404')
);
}

#[Test]
public function it_pre_processes_entry_reference_for_index()
{
$entry = Mockery::mock(\Statamic\Contracts\Entries\Entry::class);
$entry->shouldReceive('url')->once()->andReturn('/the-entry-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' => '/the-entry-url'],
$fieldtype->preProcessIndex('entry::entry-id')
);
}

#[Test]
public function it_pre_processes_asset_reference_for_index()
{
$asset = Mockery::mock(\Statamic\Contracts\Assets\Asset::class);
$asset->shouldReceive('url')->once()->andReturn('/assets/image.jpg');

Facades\Asset::shouldReceive('find')->with('main::image.jpg')->once()->andReturn($asset);

$fieldtype = (new Link)->setField(new Field('test', ['type' => 'link']));

$this->assertEquals(
['type' => 'asset', 'url' => '/assets/image.jpg'],
$fieldtype->preProcessIndex('asset::main::image.jpg')
);
}

#[Test]
public function it_pre_processes_entry_with_null_url_for_index()
{
$entry = Mockery::mock(\Statamic\Contracts\Entries\Entry::class);
$entry->shouldReceive('url')->once()->andReturnNull();

Facades\Entry::shouldReceive('find')->with('entry-id')->once()->andReturn($entry);

$fieldtype = (new Link)->setField(new Field('test', ['type' => 'link']));

$this->assertNull($fieldtype->preProcessIndex('entry::entry-id'));
}

#[Test]
public function it_pre_processes_missing_entry_reference_for_index()
{
Facades\Entry::shouldReceive('find')->with('missing-id')->once()->andReturnNull();

$fieldtype = (new Link)->setField(new Field('test', ['type' => 'link']));

$this->assertNull($fieldtype->preProcessIndex('entry::missing-id'));
}

#[Test]
public function it_pre_processes_missing_asset_reference_for_index()
{
Facades\Asset::shouldReceive('find')->with('main::missing.jpg')->once()->andReturnNull();

$fieldtype = (new Link)->setField(new Field('test', ['type' => 'link']));

$this->assertNull($fieldtype->preProcessIndex('asset::main::missing.jpg'));
}

#[Test]
public function it_pre_processes_first_child_for_index()
{
$child = Mockery::mock();
$child->shouldReceive('url')->once()->andReturn('/parent/child');

$pages = Mockery::mock();
$pages->shouldReceive('all')->once()->andReturn(collect([$child]));

$parent = Mockery::mock();
$parent->shouldReceive('isRoot')->once()->andReturn(false);
$parent->shouldReceive('pages')->once()->andReturn($pages);

$entry = Mockery::mock(\Statamic\Contracts\Entries\Entry::class);
$entry->shouldReceive('page')->once()->andReturn($parent);

$field = new Field('test', ['type' => 'link']);
$field->setParent($entry);
$fieldtype = (new Link)->setField($field);

$this->assertEquals(
['type' => 'child', 'url' => '/parent/child'],
$fieldtype->preProcessIndex('@child')
);
}

#[Test]
public function it_pre_processes_first_child_for_index_when_parent_is_root()
{
$child = Mockery::mock();
$child->shouldReceive('url')->once()->andReturn('/first-child');

$tree = Mockery::mock();
$tree->shouldReceive('pages')->once()->andReturn($tree);
$tree->shouldReceive('all')->once()->andReturn(collect(['root-page', $child])->slice(0));

$parent = Mockery::mock();
$parent->shouldReceive('isRoot')->once()->andReturn(true);
$parent->shouldReceive('locale')->once()->andReturn('en');
$parent->shouldReceive('structure')->once()->andReturn($structure = Mockery::mock());
$structure->shouldReceive('in')->with('en')->once()->andReturn($tree);

$entry = Mockery::mock(\Statamic\Contracts\Entries\Entry::class);
$entry->shouldReceive('page')->once()->andReturn($parent);

$field = new Field('test', ['type' => 'link']);
$field->setParent($entry);
$fieldtype = (new Link)->setField($field);

$this->assertEquals(
['type' => 'child', 'url' => '/first-child'],
$fieldtype->preProcessIndex('@child')
);
}

#[Test]
public function it_pre_processes_first_child_for_index_when_parent_is_not_an_entry()
{
$field = new Field('test', ['type' => 'link']);
$field->setParent(Mockery::mock());
$fieldtype = (new Link)->setField($field);

$this->assertNull($fieldtype->preProcessIndex('@child'));
}

#[Test]
public function it_pre_processes_first_child_for_index_when_no_children()
{
$pages = Mockery::mock();
$pages->shouldReceive('all')->once()->andReturn(collect());

$parent = Mockery::mock();
$parent->shouldReceive('isRoot')->once()->andReturn(false);
$parent->shouldReceive('pages')->once()->andReturn($pages);

$entry = Mockery::mock(\Statamic\Contracts\Entries\Entry::class);
$entry->shouldReceive('page')->once()->andReturn($parent);

$field = new Field('test', ['type' => 'link']);
$field->setParent($entry);
$fieldtype = (new Link)->setField($field);

$this->assertNull($fieldtype->preProcessIndex('@child'));
}
}
Loading