|
<?php |
|
|
|
namespace App\Tags; |
|
|
|
use Statamic\Tags\Tags; |
|
use Statamic\Contracts\Structures\Structure as StructureContract; |
|
use Statamic\Facades\Site; |
|
use Statamic\Facades\URL; |
|
use Statamic\Structures\TreeBuilder; |
|
use Statamic\Facades\Data; |
|
use Statamic\Support\Str; |
|
|
|
class CustomNav extends Tags |
|
{ |
|
public function wildcard($tag) |
|
{ |
|
$handle = $this->context->value($tag, $tag); |
|
|
|
// Allow {{ structure:collection:pages }} rather than needing to use the double colon. |
|
if (is_string($handle)) { |
|
$handle = str_replace(':', '::', $tag); |
|
} |
|
|
|
return $this->structure($handle); |
|
} |
|
|
|
public function index() |
|
{ |
|
return $this->structure($this->params->get('handle', 'collection::pages')); |
|
} |
|
|
|
protected function structure($handle) |
|
{ |
|
if ($handle instanceof StructureContract) { |
|
$handle = $handle->handle(); |
|
} |
|
|
|
$tree = (new TreeBuilder)->build([ |
|
'structure' => $handle, |
|
'include_home' => $this->params->get('include_home'), |
|
'show_unpublished' => $this->params->get('show_unpublished', false), |
|
'site' => $this->params->get('site', Site::current()->handle()), |
|
'from' => $this->params->get('from'), |
|
'max_depth' => $this->params->get('max_depth'), |
|
]); |
|
|
|
return $this->toArray($tree); |
|
} |
|
|
|
public function toArray($tree, $parent = null, $depth = 1) |
|
{ |
|
return collect($tree)->map(function ($item) use ($parent, $depth) { |
|
$page = $item['page']; |
|
$url = $page->url(); |
|
$data = [ |
|
'title' => $page->augmentedValue('title'), |
|
'url' => $url, |
|
]; |
|
$children = empty($item['children']) ? [] : $this->toArray($item['children'], $data, $depth + 1); |
|
|
|
return array_merge($data, [ |
|
'children' => $children, |
|
'parent' => $parent, |
|
'depth' => $depth, |
|
'is_current' => rtrim(URL::getCurrent(), '/') == rtrim($url, '/'), |
|
'is_parent' => null, |
|
'is_external' => null, |
|
// 'is_parent' => Site::current()->url() === $url ? false : URL::isAncestorOf(URL::getCurrent(), $page->urlWithoutRedirect()), |
|
// 'is_external' => URL::isExternal($page->absoluteUrl()), |
|
]); |
|
})->filter()->values()->all(); |
|
} |
|
|
|
public function breadcrumbs() |
|
{ |
|
$currentUrl = URL::makeAbsolute(URL::getCurrent()); |
|
$url = Str::removeLeft($currentUrl, Site::current()->absoluteUrl()); |
|
$url = Str::ensureLeft($url, '/'); |
|
$segments = explode('/', $url); |
|
$segments[0] = '/'; |
|
|
|
if (! $this->params->bool('include_home', true)) { |
|
array_shift($segments); |
|
} |
|
|
|
$crumbs = collect($segments)->map(function () use (&$segments) { |
|
$uri = URL::tidy(join('/', $segments)); |
|
array_pop($segments); |
|
|
|
return $uri; |
|
})->mapWithKeys(function ($uri) { |
|
$uri = Str::ensureLeft($uri, '/'); |
|
|
|
return [$uri => Data::findByUri($uri, Site::current()->handle())]; |
|
})->filter(); |
|
|
|
if (! $this->params->bool('reverse', false)) { |
|
$crumbs = $crumbs->reverse(); |
|
} |
|
|
|
if ($this->params->bool('trim', true)) { |
|
$this->content = trim($this->content); |
|
} |
|
|
|
return $crumbs->values()->map(function ($crumb) { |
|
$url = $crumb->url(); |
|
return [ |
|
'entry' => $crumb, |
|
'url' => $url, |
|
'is_current' => URL::getCurrent() === $url, |
|
]; |
|
})->all(); |
|
} |
|
} |