Skip to content

Instantly share code, notes, and snippets.

@goellner
Created November 17, 2020 18:34
Show Gist options
  • Save goellner/269e7ecadb49592b79394f5d31737ddd to your computer and use it in GitHub Desktop.
Save goellner/269e7ecadb49592b79394f5d31737ddd to your computer and use it in GitHub Desktop.
Statamic v3 navigation structure custom API
<?php
use Statamic\Http\Controllers\Controller as BaseController;
use Statamic\Facades\Structure;
use Statamic\Structures\Page;
class Controller extends BaseController
{
public function navigations()
{
$structure = Structure::findByHandle("nav_main");
$trees = $structure->trees();
$defaultSiteTree = $trees->get('default');
$pageTree["main_navigation"] = $defaultSiteTree->pages()->all()->map(function(Page $page) {
$subpages = $page->pages()->all()->map(function(Page $sub_page) {
$title = $sub_page->augmented()->get("navigation_title")->value() === NULL ? $sub_page->title() : $sub_page->augmented()->get("navigation_title");
return [
"title" => $title,
"href" => $sub_page->uri(),
"showDesktop" => $sub_page->augmented()->get("nav_show_desktop")->value() === NULL ? true : $sub_page->augmented()->get("nav_show_desktop")->value(),
"showMobile" => $sub_page->augmented()->get("nav_show_mobile")->value() === NULL ? true : $sub_page->augmented()->get("nav_show_mobile")->value()
];
});
$navstruct["title"] = $page->augmented()->get("navigation_title")->value() === NULL ? $page->title() : $page->augmented()->get("navigation_title");
$navstruct["showDesktop"] = $page->augmented()->get("nav_show_desktop")->value() === NULL ? true : $page->augmented()->get("nav_show_desktop")->value();
$navstruct["showMobile"] = $page->augmented()->get("nav_show_mobile")->value() === NULL ? true : $page->augmented()->get("nav_show_mobile")->value();
$navstruct["href"] = $page->uri();
$navstruct["children"] = $subpages;
return $navstruct;
});
return $pageTree;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment