Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[4.x] Make Link field arrayable #8911

Merged
merged 10 commits into from
Nov 3, 2023
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
30 changes: 23 additions & 7 deletions src/Fieldtypes/Link.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
use Statamic\Contracts\Entries\Entry;
use Statamic\Facades;
use Statamic\Facades\Blink;
use Statamic\Facades\GraphQL;
use Statamic\Facades\Site;
use Statamic\Fields\Field;
use Statamic\Fields\Fieldtype;
use Statamic\Fieldtypes\Link\ArrayableLink;
use Statamic\Support\Str;

class Link extends Fieldtype
Expand Down Expand Up @@ -42,13 +44,11 @@ protected function configFieldItems(): array

public function augment($value)
{
if (! $value) {
return null;
}

$redirect = ResolveRedirect::resolve($value, $this->field->parent(), true);

return $redirect === 404 ? null : $redirect;
return new ArrayableLink(
$value
? ResolveRedirect::item($value, $this->field->parent(), true)
: null
);
}

public function preload()
Expand Down Expand Up @@ -174,4 +174,20 @@ private function showAssetOption()
{
return $this->config('container') !== null;
}

public function toGqlType()
{
return [
'type' => GraphQL::string(),
'resolve' => function ($item, $args, $context, $info) {
if (! $augmented = $item->resolveGqlValue($info->fieldName)) {
return null;
}

$item = $augmented->value();

return is_object($item) ? $item->url() : $item;
},
];
}
}
31 changes: 31 additions & 0 deletions src/Fieldtypes/Link/ArrayableLink.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Statamic\Fieldtypes\Link;

use Statamic\Fields\ArrayableString;

class ArrayableLink extends ArrayableString
{
public function __toString()
{
return (string) $this->url();
}

public function toArray()
{
return is_object($this->value)
? $this->value->toAugmentedArray()
: ['url' => $this->url()];
}

#[\ReturnTypeWillChange]
public function jsonSerialize()
{
return $this->url(); // Use a string for backwards compatibility in the REST API, etc.
}

private function url()
{
return is_object($this->value) ? $this->value?->url() : $this->value;
}
}
27 changes: 21 additions & 6 deletions src/Routing/ResolveRedirect.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,33 @@ public function resolve($redirect, $parent = null, $localize = false)
return null;
}

if (! $item = $this->item($redirect, $parent, $localize)) {
return 404;
}

return is_object($item) ? $item->url() : $item;
}

public function item($redirect, $parent = null, $localize = false)
{
if (is_null($redirect)) {
return null;
}

if ($redirect === '@child') {
$redirect = $this->firstChildUrl($parent);
return $this->firstChild($parent);
}

if (Str::startsWith($redirect, 'entry::')) {
$id = Str::after($redirect, 'entry::');
$redirect = optional($this->findEntry($id, $parent, $localize))->url() ?? 404;

return $this->findEntry($id, $parent, $localize);
}

if (Str::startsWith($redirect, 'asset::')) {
$id = Str::after($redirect, 'asset::');
$redirect = optional(Facades\Asset::find($id))->url() ?? 404;

return Facades\Asset::find($id);
}

return is_numeric($redirect) ? (int) $redirect : $redirect;
Expand All @@ -56,7 +71,7 @@ private function findEntry($id, $parent, $localize)
return $entry->in($site) ?? $entry;
}

private function firstChildUrl($parent)
private function firstChild($parent)
{
if (! $parent || ! $parent instanceof Entry) {
throw new \Exception("Cannot resolve a page's child redirect without providing a page.");
Expand All @@ -71,9 +86,9 @@ private function firstChildUrl($parent)
: $parent->pages()->all();

if ($children->isEmpty()) {
return 404;
return null;
}

return $children->first()->url();
return $children->first();
}
}
18 changes: 13 additions & 5 deletions tests/Feature/GraphQL/Fieldtypes/LinkFieldtypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace Tests\Feature\GraphQL\Fieldtypes;

use Facades\Statamic\Routing\ResolveRedirect;
use Mockery;
use Statamic\Contracts\Entries\Entry;

/** @group graphql */
class LinkFieldtypeTest extends FieldtypeTestCase
Expand All @@ -17,7 +19,7 @@ public function it_gets_null_when_undefined()
],
]);

ResolveRedirect::shouldReceive('resolve')->never();
ResolveRedirect::shouldReceive('item')->never();

$this->assertGqlEntryHas('link', ['link' => null]);
}
Expand All @@ -32,7 +34,7 @@ public function it_gets_a_hardcoded_url()
],
]);

ResolveRedirect::shouldReceive('resolve')->once()->with('/hardcoded', $entry, true)->andReturn('/hardcoded');
ResolveRedirect::shouldReceive('item')->once()->with('/hardcoded', $entry, true)->andReturn('/hardcoded');

$this->assertGqlEntryHas('link', ['link' => '/hardcoded']);
}
Expand All @@ -47,7 +49,10 @@ public function it_gets_an_entry()
],
]);

ResolveRedirect::shouldReceive('resolve')->once()->with('entry::123', $entry, true)->andReturn('/the-entry-url');
$another = Mockery::mock(Entry::class);
$another->shouldReceive('url')->once()->andReturn('/the-entry-url');

ResolveRedirect::shouldReceive('item')->once()->with('entry::123', $entry, true)->andReturn($another);

$this->assertGqlEntryHas('link', ['link' => '/the-entry-url']);
}
Expand All @@ -62,7 +67,10 @@ public function it_gets_a_child_url()
],
]);

ResolveRedirect::shouldReceive('resolve')->once()->with('@child', $entry, true)->andReturn('/the-first-child');
$another = Mockery::mock(Entry::class);
$another->shouldReceive('url')->once()->andReturn('/the-first-child');

ResolveRedirect::shouldReceive('item')->once()->with('@child', $entry, true)->andReturn($another);

$this->assertGqlEntryHas('link', ['link' => '/the-first-child']);
}
Expand All @@ -77,7 +85,7 @@ public function it_gets_a_404()
],
]);

ResolveRedirect::shouldReceive('resolve')->once()->with('entry::unknown', $entry, true)->andReturn(404);
ResolveRedirect::shouldReceive('item')->once()->with('entry::unknown', $entry, true)->andReturnNull();

$this->assertGqlEntryHas('link', ['link' => null]);
}
Expand Down
54 changes: 43 additions & 11 deletions tests/Fieldtypes/LinkTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,43 +3,72 @@
namespace Tests\Fieldtypes;

use Facades\Statamic\Routing\ResolveRedirect;
use Mockery;
use Statamic\Entries\Entry;
use Statamic\Fields\ArrayableString;
use Statamic\Fields\Field;
use Statamic\Fieldtypes\Link;
use Tests\TestCase;

class LinkTest extends TestCase
{
/** @test */
public function it_augments_to_url()
public function it_augments_string_to_string()
{
ResolveRedirect::shouldReceive('resolve')
->with('entry::test', $parent = new Entry, true)
ResolveRedirect::shouldReceive('item')
->with('/foo', $parent = new Entry, true)
->once()
->andReturn('/the-redirect');
->andReturn('/foo');

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

$this->assertEquals('/the-redirect', $fieldtype->augment('entry::test'));
$augmented = $fieldtype->augment('/foo');
$this->assertInstanceOf(ArrayableString::class, $augmented);
$this->assertEquals('/foo', $augmented->value());
$this->assertEquals(['url' => '/foo'], $augmented->toArray());
}

/** @test */
public function it_augments_invalid_entry_to_null()
public function it_augments_reference_to_object()
{
// invalid entries come back from the ResolveRedirect class as a 404 integer
$entry = Mockery::mock();
$entry->shouldReceive('url')->once()->andReturn('/the-entry-url');
$entry->shouldReceive('toAugmentedArray')->once()->andReturn('augmented entry array');

ResolveRedirect::shouldReceive('resolve')
ResolveRedirect::shouldReceive('item')
->with('entry::test', $parent = new Entry, true)
->once()
->andReturn(404);
->andReturn($entry);

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

$augmented = $fieldtype->augment('entry::test');
$this->assertInstanceOf(ArrayableString::class, $augmented);
$this->assertEquals($entry, $augmented->value());
$this->assertEquals('/the-entry-url', (string) $augmented);
$this->assertEquals('augmented entry array', $augmented->toArray());
}

/** @test */
public function it_augments_invalid_object_to_null()
{
ResolveRedirect::shouldReceive('item')
->with('entry::invalid', $parent = new Entry, true)
->once()
->andReturnNull();

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

$this->assertNull($fieldtype->augment('entry::test'));
$augmented = $fieldtype->augment('entry::invalid');
$this->assertInstanceOf(ArrayableString::class, $augmented);
$this->assertNull($augmented->value());
$this->assertEquals(['url' => null], $augmented->toArray());
}

/** @test */
Expand All @@ -53,6 +82,9 @@ public function it_augments_null_to_null()
$field->setParent(new Entry);
$fieldtype = (new Link)->setField($field);

$this->assertNull($fieldtype->augment(null));
$augmented = $fieldtype->augment(null);
$this->assertInstanceOf(ArrayableString::class, $augmented);
$this->assertNull($augmented->value());
$this->assertEquals(['url' => null], $augmented->toArray());
}
}
23 changes: 16 additions & 7 deletions tests/Routing/ResolveRedirectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ public function it_resolves_first_child()
$parent->shouldReceive('pages')->andReturn($children);

$this->assertEquals('/parent/first-child', $resolver('@child', $parent));
$this->assertEquals($child, $resolver->item('@child', $parent));
}

/** @test */
Expand All @@ -87,6 +88,7 @@ public function it_resolves_first_child_through_an_entry()
$parent->shouldReceive('page')->andReturn($parentPage);

$this->assertEquals('/parent/first-child', $resolver('@child', $parent));
$this->assertEquals($child, $resolver->item('@child', $parent));
}

/** @test */
Expand Down Expand Up @@ -116,6 +118,7 @@ public function it_resolves_a_first_child_redirect_when_its_a_root_page()
$root->shouldReceive('structure')->andReturn($structure);

$this->assertEquals('/parent/first-child', $resolver('@child', $root));
$this->assertEquals($child, $resolver->item('@child', $root));
}

/** @test */
Expand All @@ -131,6 +134,7 @@ public function a_parent_without_a_child_resolves_to_a_404()
$parent->shouldReceive('pages')->andReturn($pages);

$this->assertSame(404, $resolver('@child', $parent));
$this->assertSame(null, $resolver->item('@child', $parent));
}

/** @test */
Expand All @@ -139,9 +143,10 @@ public function it_resolves_references_to_entries()
$resolver = new ResolveRedirect;

$entry = Mockery::mock(Entry::class)->shouldReceive('url')->once()->andReturn('/the-entry')->getMock();
Facades\Entry::shouldReceive('find')->with('123')->once()->andReturn($entry);
Facades\Entry::shouldReceive('find')->with('123')->twice()->andReturn($entry);

$this->assertEquals('/the-entry', $resolver('entry::123'));
$this->assertEquals($entry, $resolver->item('entry::123'));
}

/** @test */
Expand All @@ -151,10 +156,11 @@ public function it_resolves_references_to_entries_localized()

$parentEntry = Mockery::mock(Entry::class);
$frenchEntry = Mockery::mock(Entry::class)->shouldReceive('url')->once()->andReturn('/le-entry')->getMock();
$defaultEntry = Mockery::mock(Entry::class)->shouldReceive('in')->once()->andReturn($frenchEntry)->getMock();
Facades\Entry::shouldReceive('find')->with('123')->once()->andReturn($defaultEntry);
$defaultEntry = Mockery::mock(Entry::class)->shouldReceive('in')->twice()->andReturn($frenchEntry)->getMock();
Facades\Entry::shouldReceive('find')->with('123')->twice()->andReturn($defaultEntry);

$this->assertEquals('/le-entry', $resolver('entry::123', $parentEntry, true));
$this->assertEquals($frenchEntry, $resolver->item('entry::123', $parentEntry, true));
}

/** @test */
Expand All @@ -164,11 +170,12 @@ public function it_resolves_references_to_entries_localized_with_fallback()

$parentEntry = Mockery::mock(Entry::class);
$entry = Mockery::mock(Entry::class);
$entry->shouldReceive('in')->once()->andReturn(null);
$entry->shouldReceive('in')->twice()->andReturn(null);
$entry->shouldReceive('url')->once()->andReturn('/the-entry');
Facades\Entry::shouldReceive('find')->with('123')->once()->andReturn($entry);
Facades\Entry::shouldReceive('find')->with('123')->twice()->andReturn($entry);

$this->assertEquals('/the-entry', $resolver('entry::123', $parentEntry, true));
$this->assertEquals($entry, $resolver->item('entry::123', $parentEntry, true));
}

/** @test */
Expand All @@ -177,19 +184,21 @@ public function it_resolves_references_to_assets()
$resolver = new ResolveRedirect;

$asset = Mockery::mock(Asset::class)->shouldReceive('url')->once()->andReturn('/assets/foo/bar/baz.jpg')->getMock();
Facades\Asset::shouldReceive('find')->with('foo::bar/baz.jpg')->once()->andReturn($asset);
Facades\Asset::shouldReceive('find')->with('foo::bar/baz.jpg')->twice()->andReturn($asset);

$this->assertEquals('/assets/foo/bar/baz.jpg', $resolver('asset::foo::bar/baz.jpg'));
$this->assertEquals($asset, $resolver->item('asset::foo::bar/baz.jpg'));
}

/** @test */
public function unknown_entry_ids_resolve_to_404()
{
$resolver = new ResolveRedirect;

Facades\Entry::shouldReceive('find')->with('123')->once()->andReturnNull();
Facades\Entry::shouldReceive('find')->with('123')->twice()->andReturnNull();

$this->assertSame(404, $resolver('entry::123'));
$this->assertSame(null, $resolver->item('entry::123'));
}

/** @test */
Expand Down
Loading