Skip to content

Instantly share code, notes, and snippets.

@ryanmitchell
Created May 26, 2022 09:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryanmitchell/c385bbb0b505cd80fe88aa86af036b9c to your computer and use it in GitHub Desktop.
Save ryanmitchell/c385bbb0b505cd80fe88aa86af036b9c to your computer and use it in GitHub Desktop.
Statamic {{ nav_from }} tag
<?php
namespace App\Tags;
use Statamic\Support\Str;
use Statamic\Tags\Nav;
class NavFrom extends Nav
{
/**
* The {{ nav_from }} tag.
*
* @return string|array
*/
public function index()
{
$nav = parent::index();
$from_uri = $this->params->get('from_uri');
if (!$from_uri)
return $nav;
$segments = explode('/', Str::removeLeft($from_uri, '/'));
$segment = '';
while (count($segments) > 0) {
$segment .= '/'.array_shift($segments);
$nav = collect($nav)->first(function ($entry) use ($segment) {
return $entry['uri'] == $segment;
});
if (!$nav || !count($nav['children']))
return collect(['no_results' => true]);
$nav = $nav['children'];
}
return $nav;
}
}
<!-- use like -->
{{ nav_from handle="xxx" :from_uri="whatever/page/to/start/at" }}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment