Skip to content

Commit

Permalink
ENHANCEMENT Hierarchy->Breadcrumbs()
Browse files Browse the repository at this point in the history
  • Loading branch information
chillu committed Mar 2, 2012
1 parent fda7a6a commit e5c4b0a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
15 changes: 15 additions & 0 deletions model/Hierarchy.php
Expand Up @@ -605,6 +605,21 @@ public function getAncestors() {

return $ancestors;
}

/**
* Returns a human-readable, flattened representation of the path to the object,
* using its {@link Title()} attribute.
*
* @param String
* @return String
*/
public function getBreadcrumbs($separator = ' » ') {
$crumbs = array();
$ancestors = array_reverse($this->owner->getAncestors()->toArray());
foreach($ancestors as $ancestor) $crumbs[] = $ancestor->Title;
$crumbs[] = $this->owner->Title;
return implode($separator, $crumbs);
}

/**
* Get the next node in the tree of the type. If there is no instance of the className descended from this node,
Expand Down
11 changes: 11 additions & 0 deletions tests/model/HierarchyTest.php
Expand Up @@ -150,6 +150,17 @@ function testLiveChildrenOnlyDeletedFromStage() {
$this->assertNotContains("Obj 2b", $children);
}

function testBreadcrumbs() {
$obj1 = $this->objFromFixture('HierarchyTest_Object', 'obj1');
$obj2 = $this->objFromFixture('HierarchyTest_Object', 'obj2');
$obj2a = $this->objFromFixture('HierarchyTest_Object', 'obj2a');
$obj2aa = $this->objFromFixture('HierarchyTest_Object', 'obj2aa');

$this->assertEquals('Obj 1', $obj1->getBreadcrumbs());
$this->assertEquals('Obj 2 » Obj 2a', $obj2a->getBreadcrumbs());
$this->assertEquals('Obj 2 » Obj 2a » Obj 2aa', $obj2aa->getBreadcrumbs());
}

}

class HierarchyTest_Object extends DataObject implements TestOnly {
Expand Down

1 comment on commit e5c4b0a

@sminnee
Copy link
Member

@sminnee sminnee commented on e5c4b0a Mar 5, 2012

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good, although you may not always want to use Title as the breadcrumb title.

Please sign in to comment.