Menu protocol
Is used to create a menu, set a protocol that is used later to validate pages 404 and 301 redirect.
The order in the methods bellow is called is important.
Create menu
1. Load items from database
Load database items and init builder.
$select = \query\DB::_select("id,name,permalink,parent,template", "pages", ["CON_DEFAULT", "type" => "page"], "position ASC"); $fetch = $select->fetch(function($row) { return [$row->parent => [$row->id => $row]]; }); $builder = new \nest\builder($fetch);
2. Build URI menu structure
Build backend URI menu structure, here you can customize the premalink for each item.
// If you want to prepend URI. This should be called before build $builder->uriAppend(["blogg"]); $builder->build(function($obj) { // Add to URI protocol. // IF false then take previous URI value, can be used with section/anchor, // will return page URI, then you can later add the anchor return $obj->permalink; });
Once it is build you can use the same meny as regular menu and sidebar without loading above.
2. Get protocol
Protocol will be used to validate current page. You need to set protocol before building template bellow so active menu item will work.
$this->modal()->protocol() = $builder->protocol();
2.1 Modify item in protocol
You can modify the URI for a item in protocol.
@builder->protocol()->modifyProtocol(PAGE_ID, URI_ARRAY_PART, TYPE)
TYPE
- append: about-us/our-blogg-page => about-us/our-blogg-page/blogg
- prepend: about-us/our-blogg-page => blogg/about-us/our-blogg-page
- change: about-us/our-blogg-page => about-us/blogg
- replace: about-us/our-blogg-page => blogg
$this->modal()->protocol()->modifyProtocol(1, ["blogg"], "change");
2.2 Set current page in protocol
Protocol will be used to validate current page. You need to set protocol before building template bellow so active menu item will work.
@builder->protocol()->get(URI_ROUTER_ARRAY_PART)
$uriPart = $this->url()->select("page")->get(); $this->modal()->protocol()->get($uriPart);
3. Set menu template
KEY is used so you can choose nav template when echo menu.
@builder->html(SET_TEMPLATE_NAME, UL_TAG, LI_TAG, CALLBACK)
// Default navigation template, args: template name, listparent tag, list tag, callback $builder->html("nav", "ul", "li", function($obj, $li, $active, $level) { // Has children? $hasChild = ($obj->hasChild) ? " has-child" : NULL; // Add class to li tag $li->attr("class", "menuitem{$hasChild}{$active}"); // Create a tag (link) $a = $li->create("a", $obj->name) ->attr("href", $obj->uri) ->attr("class", "item{$active}"); });
4. echo menu
@builder->get(GET_TEMPLATE_NAME)
echo $builder->get("nav");
5. echo sidebar menu after top menu
@builder->get(GET_TEMPLATE_NAME)
if(!is_null($this->modal()->protocol()) && ($arr = $this->modal()->protocol()->data()) && count($arr) > 1) { $obj = reset($arr); $topParent = key($arr); echo $builder->get("nav", $topParent); }
Multiple menus
Modify/add the bolded text bellow into the above code.
1. Load items from database
$select = \query\DB::_select("id,name,permalink,parent,template", "pages", ["CON_DEFAULT", "type" => "page"], "position ASC"); $fetch = $select->fetch(function($row) { return [$row->mid => [$row->parent => [$row->id => $row]]]; }); $builder = new \nest\builder($fetch);
2. Build URI menu structure
$builder->multiple()->build(function($obj) { // Add to URI protocol return $obj->permalink; });
4. echo menu
@builder->select(MENU_ID)->get(GET_TEMPLATE_NAME)
// Default menu echo $builder->select(0)->get("nav"); // New menu echo $builder->select(1)->get("nav");
Validate current page
Now the menu and protocol is set
switch($this->modal()->protocol()->status()) { case 301: $this->modal()->headers()->set(301); $this->modal()->headers()->location(301); // Instantly kill processed and redirect the users location. You can tho skip this, it will be executed later on. // $this->modal()->headers()->send(); break; case 404: $this->modal()->headers()->set(404); break; default: $breadcrumb = $this->modal()->protocol()->data(); $current = end($pages); break; }