From 3264e655c90a4846543611985371c928cbfb9741 Mon Sep 17 00:00:00 2001 From: Milos Levacic Date: Sat, 25 Oct 2014 21:02:15 +0200 Subject: [PATCH] Add ability to pass custom data into breadcrumbs Closes #35 --- README.md | 22 +++++++++++++++++++ src/DaveJamesMiller/Breadcrumbs/Generator.php | 6 ++--- tests/GeneratorTest.php | 17 ++++++++++++++ 3 files changed, 42 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index c747084..9f7cb28 100644 --- a/README.md +++ b/README.md @@ -337,6 +337,28 @@ The default Twitter Bootstrap templates provided render this with a CSS class of "active", the same as the last breadcrumb, because otherwise they default to black text not grey which doesn't look right. +### Breadcrumbs with custom data + +The `push()` method accepts an optional third parameter, `$data` - an array of +arbitrary data to be passed to the breadcrumb, which you can later use in your +custom template. For example, if you wanted each breadcrumb to have an icon, you +could do: + +```php +$breadcrumbs->push('Home', '/', array('icon' => '/images/icons/home.png')); +``` + +The `$data` array's entries will be merged into the breadcrumb as properties, so +you would access the icon as `$breadcrumb->icon` in your template, like this: + +```html+php +
  • {{{ $breadcrumb->title }}}
  • +``` + +Some default properties are added to the breadcrumb by the package, so to avoid +running into problems, do not use the following keys in your data array, as they +will be overwritten: `title`, `url`, `first`, `last`. + ### Defining breadcrumbs in a different file If you don't want to use `app/breadcrumbs.php`, you can define them in diff --git a/src/DaveJamesMiller/Breadcrumbs/Generator.php b/src/DaveJamesMiller/Breadcrumbs/Generator.php index 928488d..b6a6df6 100644 --- a/src/DaveJamesMiller/Breadcrumbs/Generator.php +++ b/src/DaveJamesMiller/Breadcrumbs/Generator.php @@ -46,15 +46,15 @@ public function parentArray($name, $params = array()) $this->call($name, $params); } - public function push($title, $url = null) + public function push($title, $url = null, array $data = array()) { - $this->breadcrumbs[] = (object) array( + $this->breadcrumbs[] = (object) array_merge($data, array( 'title' => $title, 'url' => $url, // These will be altered later where necessary: 'first' => false, 'last' => false, - ); + )); } public function toArray() diff --git a/tests/GeneratorTest.php b/tests/GeneratorTest.php index 11967b9..813b78f 100644 --- a/tests/GeneratorTest.php +++ b/tests/GeneratorTest.php @@ -61,6 +61,23 @@ public function testPushWithoutUrl() $this->assertNull($breadcrumbs[0]->url); } + public function testPushWithData() + { + $data = array( + 'foo' => 'bar', + 'baz' => 'qux', + 'title' => 'should not be overwritten by custom data', + ); + + $generator = new Generator(array()); + $generator->push('Home', '/', $data); + $breadcrumbs = $generator->get(); + + $this->assertSame('bar', $breadcrumbs[0]->foo); + $this->assertSame('qux', $breadcrumbs[0]->baz); + $this->assertSame('Home', $breadcrumbs[0]->title); + } + public function testToArray() { $generator = new Generator(array());