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
10 changes: 10 additions & 0 deletions src/Structures/AugmentedPage.php
Original file line numberDiff line numberDiff line change
Expand Up@@ -61,6 +61,16 @@ protected function getFromData($key)
return $this->page->getSupplement($key) ?? $this->page->value($key);
}

protected function url()
{
return $this->page->url();
}

protected function urlWithoutRedirect()
{
return $this->page->urlWithoutRedirect();
}

public function blueprintFields()
{
if ($this->fieldsCache) {
Expand Down
29 changes: 27 additions & 2 deletions src/Structures/Page.php
Original file line numberDiff line numberDiff line change
Expand Up@@ -79,12 +79,37 @@ public function setUrl($url)

public function url()
{
return $this->url ?? optional($this->entry())->url();
if ($this->url) {
return $this->url;
}

if (! $entry = $this->entry()) {
return null;
}

return $this->linksToAnotherSite($entry) ? $entry->absoluteUrl() : $entry->url();
}

public function urlWithoutRedirect()
{
return $this->url ?? optional($this->entry())->urlWithoutRedirect();
if ($this->url) {
return $this->url;
}

if (! $entry = $this->entry()) {
return null;
}

return $this->linksToAnotherSite($entry) ? $entry->absoluteUrlWithoutRedirect() : $entry->urlWithoutRedirect();
}

private function linksToAnotherSite(Entry $entry)
{
if (! $this->structure() instanceof Nav || ! $this->structure()->canSelectAcrossSites()) {
return false;
}

return $entry->site()->handle() !== $this->tree->site()->handle();
}

public function isRedirect()
Expand Down
1 change: 1 addition & 0 deletions tests/Data/Structures/AugmentedPageTest.php
Original file line numberDiff line numberDiff line change
Expand Up@@ -200,6 +200,7 @@ public function it_gets_values_from_the_entry()
$page->shouldReceive('data')->andReturn(collect(['one' => 'dos', 'three' => 'quatro', 'five' => 'seis']));
$page->shouldReceive('supplements')->andReturn(collect(['seven' => 'ocho']));
$page->shouldReceive('title')->andReturn('The Page Title');
$page->shouldReceive('url')->andReturn('/the-url');
$page->shouldReceive('value')->with('one')->andReturn('dos');
$page->shouldReceive('value')->with('three')->andReturn('quatro');
$page->shouldReceive('value')->with('five')->andReturn('seis');
Expand Down
14 changes: 8 additions & 6 deletions tests/Data/Structures/PageTest.php
Original file line numberDiff line numberDiff line change
Expand Up@@ -243,9 +243,10 @@ public function it_gets_the_entrys_uri_when_the_structure_does_not_have_a_collec
$entry->shouldReceive('uri')->andReturn('/the/actual/entry/uri');
$entry->shouldReceive('value')->with('redirect')->andReturnNull();

$tree = $this->newTree()->setStructure(
$this->mock(Nav::class)
);
$nav = $this->mock(Nav::class);
$nav->shouldReceive('canSelectAcrossSites')->andReturnFalse();

$tree = $this->newTree()->setStructure($nav);

$page = (new Page)
->setTree($tree)
Expand All@@ -269,9 +270,10 @@ public function it_gets_the_uri_of_a_redirect_entry()
$entry->shouldReceive('uri')->andReturn('/the/actual/entry/uri');
$entry->shouldReceive('value')->with('redirect')->andReturn('http://example.com/page');

$tree = $this->newTree()->setStructure(
$this->mock(Nav::class)
);
$nav = $this->mock(Nav::class);
$nav->shouldReceive('canSelectAcrossSites')->andReturnFalse();

$tree = $this->newTree()->setStructure($nav);

$page = (new Page)
->setTree($tree)
Expand Down
47 changes: 47 additions & 0 deletions tests/Tags/StructureTagTest.php
Original file line numberDiff line numberDiff line change
Expand Up@@ -9,6 +9,7 @@
use Statamic\Facades\Collection;
use Statamic\Facades\Entry;
use Statamic\Facades\Nav;
use Statamic\Facades\Site;
use Tests\PreventSavingStacheItemsToDisk;
use Tests\TestCase;

Expand DownExpand Up@@ -455,6 +456,52 @@ public function it_sets_is_current_and_is_parent_for_a_nav_when_home_is_an_entry
$this->assertEquals('[home][1=parent][1-1=parent][1-1-1=current][1-1-1-1][2][3]', $result);
}

#[Test]
public function it_uses_the_absolute_url_for_a_nav_entry_link_on_another_site()
{
$this->makeCrossSiteNav();

$template = '{{ nav:test }}[{{ id }}={{ url }}]{{ /nav:test }}';

$this->assertEquals('[link=http://two.example.com/projects][local-link=/projects]', (string) Antlers::parse($template, [], true));
}

#[Test]
public function it_only_flags_the_local_nav_entry_link_as_current()
{
$this->makeCrossSiteNav();

$mock = \Mockery::mock(\Statamic\Facades\URL::getFacadeRoot())->makePartial();
\Statamic\Facades\URL::swap($mock);
$mock->shouldReceive('getCurrent')->once()->andReturn('/projects');

$template = '{{ nav:test }}[{{ id }}{{ if is_current }}=current{{ /if }}]{{ /nav:test }}';

$this->assertEquals('[link][local-link=current]', (string) Antlers::parse($template, [], true));
}

private function makeCrossSiteNav()
{
$this->setSites([
'en' => ['url' => 'http://one.example.com/', 'locale' => 'en'],
'fr' => ['url' => 'http://two.example.com/', 'locale' => 'fr'],
]);

Site::setCurrent('en');

tap(Collection::make('pages')->routes('{slug}'))->sites(['en', 'fr'])->save();

EntryFactory::collection('pages')->id('projects')->locale('en')->slug('projects')->data(['title' => 'Projects'])->create();
EntryFactory::collection('pages')->id('projects-fr')->origin('projects')->locale('fr')->slug('projects')->data(['title' => 'Projects'])->create();

$nav = Nav::make('test')->canSelectAcrossSites(true);
$nav->makeTree('en', [
['id' => 'link', 'title' => 'Projects', 'entry' => 'projects-fr'],
['id' => 'local-link', 'title' => 'Projects (local)', 'entry' => 'projects'],
])->save();
$nav->save();
}

#[Test]
public function it_sets_is_parent_based_on_the_url_too()
{
Expand Down
Loading