Skip to content

Commit

Permalink
Add ability to pass custom data into breadcrumbs
Browse files Browse the repository at this point in the history
Closes d13r#35
  • Loading branch information
levacic committed Oct 25, 2014
1 parent a658ae7 commit 3264e65
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 3 deletions.
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
<li><a href="{{{ $breadcrumb->url }}}"><img src="{{{ $breadcrumb->icon }}}">{{{ $breadcrumb->title }}}</a></li>
```

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
Expand Down
6 changes: 3 additions & 3 deletions src/DaveJamesMiller/Breadcrumbs/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
17 changes: 17 additions & 0 deletions tests/GeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down

0 comments on commit 3264e65

Please sign in to comment.