ENHANCEMENT Hierarchy->Breadcrumbs()

This commit is contained in:
Ingo Schommer 2012-03-02 17:10:47 +01:00
parent fda7a6aafe
commit e5c4b0a36b
2 changed files with 26 additions and 0 deletions

View File

@ -605,6 +605,21 @@ class Hierarchy extends DataExtension {
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,

View File

@ -150,6 +150,17 @@ class HierarchyTest extends SapphireTest {
$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 {