Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #974 from micmania1/930-added-breadcrumbs-getter

API Added getBreadcrumbItems to SiteTree
  • Loading branch information...
commit cba0061fe2cfb621ae0c01aed9091441e86a15a2 2 parents 6a69134 + f46b6bd
Damian Mooyman tractorcow authored
30 code/model/SiteTree.php 100644 → 100755
View
@@ -718,12 +718,31 @@ public function duplicateAsChild($id) {
* (with ShowInMenus=0).
*
* @param int $maxDepth The maximum depth to traverse.
- * @param boolean $unlinked Do not make page names links
+ * @param boolean $unlinked Whether to link page titles.
* @param boolean|string $stopAtPageType ClassName of a page to stop the upwards traversal.
* @param boolean $showHidden Include pages marked with the attribute ShowInMenus = 0
* @return HTMLText The breadcrumb trail.
*/
public function Breadcrumbs($maxDepth = 20, $unlinked = false, $stopAtPageType = false, $showHidden = false) {
+ $pages = $this->getBreadcrumbItems($maxDepth, $stopAtPageType, $showHidden);
+ $template = new SSViewer('BreadcrumbsTemplate');
+ return $template->process($this->customise(new ArrayData(array(
+ "Pages" => $pages,
+ "Unlinked" => $unlinked
+ ))));
+ }
+
+
+ /**
+ * Returns a list of breadcrumbs for the current page.
+ *
+ * @param int $maxDepth The maximum depth to traverse.
+ * @param boolean|string $stopAtPageType ClassName of a page to stop the upwards traversal.
+ * @param boolean $showHidden Include pages marked with the attribute ShowInMenus = 0
+ *
+ * @return ArrayList
+ */
+ public function getBreadcrumbItems($maxDepth = 20, $stopAtPageType = false, $showHidden = false) {
$page = $this;
$pages = array();
@@ -738,14 +757,11 @@ public function Breadcrumbs($maxDepth = 20, $unlinked = false, $stopAtPageType =
$page = $page->Parent;
}
-
- $template = new SSViewer('BreadcrumbsTemplate');
-
- return $template->process($this->customise(new ArrayData(array(
- 'Pages' => new ArrayList(array_reverse($pages))
- ))));
+
+ return new ArrayList(array_reverse($pages));
}
+
/**
* Make this page a child of another page.
*
5 templates/BreadcrumbsTemplate.ss 100644 → 100755
View
@@ -1,5 +1,4 @@
+<%-- Loop is all on one line to prevent whitespace bug in older versions of IE --%>
<% if $Pages %>
- <% loop $Pages %>
- <% if $Last %>$MenuTitle.XML<% else %><a href="$Link" class="breadcrumb-$Pos">$MenuTitle.XML</a> &raquo;<% end_if %>
- <% end_loop %>
+ <% loop $Pages %><% if $Last %>$MenuTitle.XML<% else %><% if not Up.Unlinked %><a href="$Link" class="breadcrumb-$Pos"><% end_if %>$MenuTitle.XML<% if not Up.Unlinked %></a><% end_if %> &raquo; <% end_if %><% end_loop %>
<% end_if %>
19 tests/model/SiteTreeTest.php
View
@@ -943,6 +943,25 @@ public function testMetaTagGeneratorDisabling() {
// reset original value
Config::inst()->update('SiteTree', 'meta_generator', $generator);
}
+
+
+ public function testGetBreadcrumbItems() {
+ $page = $this->objFromFixture("Page", "breadcrumbs");
+ $this->assertEquals($page->getBreadcrumbItems()->count(), 1, "Only display current page.");
+
+ // Test breadcrumb order
+ $page = $this->objFromFixture("Page", "breadcrumbs5");
+ $breadcrumbs = $page->getBreadcrumbItems();
+ $this->assertEquals($breadcrumbs->count(), 5, "Display all breadcrumbs");
+ $this->assertEquals($breadcrumbs->first()->Title, "Breadcrumbs", "Breadcrumbs should be the first item.");
+ $this->assertEquals($breadcrumbs->last()->Title, "Breadcrumbs 5", "Breadcrumbs 5 should be last item.");
+
+ // Test breadcrumb max depth
+ $breadcrumbs = $page->getBreadcrumbItems(2);
+ $this->assertEquals($breadcrumbs->count(), 2, "Max depth should limit the breadcrumbs to 2 items.");
+ $this->assertEquals($breadcrumbs->first()->Title, "Breadcrumbs 4", "First item should be Breadrcumbs 4.");
+ $this->assertEquals($breadcrumbs->last()->Title, "Breadcrumbs 5", "Breadcrumbs 5 should be last.");
+ }
/**
* Tests SiteTree::MetaTags
14 tests/model/SiteTreeTest.yml 100644 → 100755
View
@@ -89,6 +89,20 @@ Page:
Title: 'HTML & XML'
MetaDescription: 'The <br /> and <br> tags'
ExtraMeta: '<link rel="canonical" href="http://www.mysite.com/html-and-xml" />'
+ breadcrumbs:
+ Title: 'Breadcrumbs'
+ breadcrumbs2:
+ Title: 'Breadcrumbs 2'
+ Parent: =>Page.breadcrumbs
+ breadcrumbs3:
+ Title: 'Breadcrumbs 3'
+ Parent: =>Page.breadcrumbs2
+ breadcrumbs4:
+ Title: 'Breadcrumbs 4'
+ Parent: =>Page.breadcrumbs3
+ breadcrumbs5:
+ Title: 'Breadcrumbs 5'
+ Parent: =>Page.breadcrumbs4
SiteTreeTest_Conflicted:
parent:
Please sign in to comment.
Something went wrong with that request. Please try again.