Hi DCP fans, I share my view template from my work.
<?phpdefined('C5_EXECUTE') or die(_("Access Denied."));?><?php/* This block was made with Designer Content Pro. Visithttp://theblockery.com/dcp for documentation. */?><?php/**
* link: Link Field
* css_class: Textbox field
*/// Get current page object$current= Page::getCurrentPage();// Get repeating items$items=$controller->getRepeatingItems();// Check if items existif(count($items)>0):?>
<nav class="dcp-manual-nav" role="navigation">
<ul>
<?phpdefined('C5_EXECUTE') or die(_("Access Denied."));?><?php/* This block was made with Designer Content Pro. Visithttp://theblockery.com/dcp for documentation. */?><?php/**
* link: Link Field
* css_class: Textbox field
*/// Get current page object$current= Page::getCurrentPage();// Get repeating items$items=$controller->getRepeatingItems();// Check if items existif(count($items)>0):?>
<nav class="dcp-manual-nav" role="navigation">
<ul>
<?phpforeach($itemsas$item):// Get field values$title=$item->link->getText();$url=$item->link->getHref();$page=$item->link->getPageObj();// to get page object, you need DCP 1.2+// Determine CSS classes$classes=array();// Check if the link is c5 Pageif(is_object($page)){$target=($page->getCollectionPointerExternalLink()!=''&&$page->openCollectionPointerExternalLinkInNewWindow()) ? '_blank':$page->getAttribute('nav_target');$target=empty($target) ? '_self':$target;// Create an array of parent cIDs so we can determine the "nav path" of the current page$inspectC=$current;$selectedPathCIDs=array($inspectC->getCollectionID());$parentCIDnotZero=true;while($parentCIDnotZero){$cParentID=$inspectC->cParentID;if(!intval($cParentID)){$parentCIDnotZero=false;}else{if($cParentID!= HOME_CID){$selectedPathCIDs[]=$cParentID;//Don't want home page in nav-path-selected}$inspectC= Page::getById($cParentID,'ACTIVE');}}// Current/ancestor page$selected=false;$path_selected=false;if($current->getCollectionID()==$page->getCollectionID()){$selected=true;// Current item is the page being viewed$path_selected=true;}elseif(in_array($page->getCollectionID(),$selectedPathCIDs)){$path_selected=true;// Current item is an ancestor of the page being viewed}if($selected){// class for the page currently being viewed$classes[]='nav-selected';}if($path_selected){// class for parent items of the page currently being viewed$classes[]='nav-path-selected';}}else{// the link is external link$target='_blank';}if($item->css_class->getText()){// class from the item field$classes[]=$item->css_class->getText();}// Put all classes together into one space-separated string$class=implode(" ",$classes);?>
<li><a href="<?phpecho$url?>" class="<?phpecho$class?>" target="<?phpecho$target?>"><?phpecho$title?></a></li>
<?phpendforeach;?>
</ul>
</nav>
<?phpendif;
To improve this template more designer friendly, we need $item->link->isSelected() and $item->link->isPathSelected() API in DcpFieldDisplay_Link class. I expect this.
Thanks for posting this. Here is a more simplified version of that code, which uses the page paths for comparison (it cuts down on a lot of logic this way):
<?phpdefined('C5_EXECUTE') or die(_("Access Denied."));?><?php/* This block was made with Designer Content Pro. Visithttp://theblockery.com/dcp for documentation. */?><?php/**
* link: Link Field
* css_class: Textbox field
*/// Get current page path$current_path= Page::getCurrentPage()->getCollectionPath();// Get repeating items$items=$controller->getRepeatingItems();// Check if items existif(count($items)>0):?>
<nav class="dcp-manual-nav" role="navigation">
<ul>
<?phpdefined('C5_EXECUTE') or die(_("Access Denied."));?><?php/* This block was made with Designer Content Pro. Visithttp://theblockery.com/dcp for documentation. */?><?php/**
* link: Link Field
* css_class: Textbox field
*/// Get current page path$current_path= Page::getCurrentPage()->getCollectionPath();// Get repeating items$items=$controller->getRepeatingItems();// Check if items existif(count($items)>0):?>
<nav class="dcp-manual-nav" role="navigation">
<ul>
<?phpforeach($itemsas$item):// Get field values$title=$item->link->getText();$url=$item->link->getHref();// Determine CSS classes$classes=array();// Check if the link is c5 Pageif($item->link->isPageLink()){$page=$item->link->getPageObj();// to get page object, you need DCP 1.2+$target=($page->getCollectionPointerExternalLink()!=''&&$page->openCollectionPointerExternalLinkInNewWindow()) ? '_blank':$page->getAttribute('nav_target');$target=empty($target) ? '_self':$target;// get the nav menu item's path (for comparison to the current page path)$item_path=$item->link->getPageObj()->getCollectionPath();if($item_path==$current_path){// class for the page currently being viewed$classes[]='nav-selected';}if(!empty($item_path)&&(strpos($current_path,$item_path)===0)){// class for parent items of the page currently being viewed (except the home page)$classes[]='nav-path-selected';}}else{// the link is external link$target='_blank';}if($item->css_class->getText()){// class from the item field$classes[]=$item->css_class->getText();}// Put all classes together into one space-separated string$class=implode(" ",$classes);?>
<li><a href="<?phpecho$url?>" class="<?phpecho$class?>" target="<?phpecho$target?>"><?phpecho$title?></a></li>
<?phpendforeach;?>
</ul>
</nav>
<?phpendif;
And I think your suggestion for "isSelected" and "isPathSelected" is a good one -- I will consider adding this to the next version.
And I think your suggestion for "isSelected" and "isPathSelected" is a good one -- I will consider adding this to the next version.
-Jordan