mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
ENHANCEMENT Hierarchy->Breadcrumbs()
This commit is contained in:
parent
fda7a6aafe
commit
e5c4b0a36b
@ -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,
|
||||
|
@ -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 {
|
||||
|
Loading…
Reference in New Issue
Block a user