Skip to content

Instantly share code, notes, and snippets.

@ryanmitchell
Created November 15, 2021 21:13
Show Gist options
  • Save ryanmitchell/01b8566098ef078750962c77cefd2e62 to your computer and use it in GitHub Desktop.
Save ryanmitchell/01b8566098ef078750962c77cefd2e62 to your computer and use it in GitHub Desktop.
Statamic NavFrom
<?php
namespace App\Tags;
use Statamic\Support\Str;
use Statamic\Tags\Nav;
use Statamic\Tags\Tags;
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;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment