Skip to content

Commit

Permalink
Enhance HtmlHelper::getCrumbs() $startText param
Browse files Browse the repository at this point in the history
It now accepts an array which gives more control and flexibilibity
over the first breadcrumb link.

Fixes #2475
  • Loading branch information
markstory committed Jan 14, 2012
1 parent 7badb1d commit c89c49c
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 8 deletions.
31 changes: 25 additions & 6 deletions lib/Cake/Test/Case/View/Helper/HtmlHelperTest.php
Expand Up @@ -866,7 +866,7 @@ public function testCharsetTag() {
}

/**
* testBreadcrumb method
* testGetCrumb and addCrumb method
*
* @return void
*/
Expand Down Expand Up @@ -909,11 +909,6 @@ public function testBreadcrumb() {
);
$this->assertTags($result, $expected);

$this->assertRegExp('/^<a[^<>]+>First<\/a> &gt; <a[^<>]+>Second<\/a> &gt; <a[^<>]+>Third<\/a>$/', $result);
$this->assertRegExp('/<a\s+href=["\']+\#first["\']+[^<>]*>First<\/a>/', $result);
$this->assertRegExp('/<a\s+href=["\']+\#second["\']+[^<>]*>Second<\/a>/', $result);
$this->assertRegExp('/<a\s+href=["\']+\#third["\']+[^<>]*>Third<\/a>/', $result);
$this->assertNotRegExp('/<a[^<>]+[^href]=[^<>]*>/', $result);

$this->Html->addCrumb('Fourth', null);

Expand Down Expand Up @@ -958,6 +953,30 @@ public function testBreadcrumb() {
$this->assertTags($result, $expected);
}

/**
* Test the array form of $startText
*/
public function testGetCrumbFirstLink() {
$this->Html->addCrumb('First', '#first');
$this->Html->addCrumb('Second', '#second');

$result = $this->Html->getCrumbs(' - ', array('url' => '/home', 'text' => '<img src="/home.png" />', 'escape' => false));
$expected = array(
array('a' => array('href' => '/home')),
'img' => array('src' => '/home.png'),
'/a',
' - ',
array('a' => array('href' => '#first')),
'First',
'/a',
' - ',
array('a' => array('href' => '#second')),
'Second',
'/a',
);
$this->assertTags($result, $expected);
}

/**
* testNestedList method
*
Expand Down
22 changes: 20 additions & 2 deletions lib/Cake/View/Helper/HtmlHelper.php
Expand Up @@ -658,16 +658,34 @@ public function style($data, $oneline = true) {
/**
* Returns the breadcrumb trail as a sequence of &raquo;-separated links.
*
* If `$startText` is an array, the accepted keys are:
*
* - `text` Define the text/content for the link.
* - `url` Define the target of the created link.
*
* All other keys will be passed to HtmlHelper::link() as the `$options` parameter.
*
* @param string $separator Text to separate crumbs.
* @param string $startText This will be the first crumb, if false it defaults to first crumb in array
* @param mixed $startText This will be the first crumb, if false it defaults to first crumb in array. Can
* also be an array, see above for details.
* @return string Composed bread crumbs
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/html.html#creating-breadcrumb-trails-with-htmlhelper
*/
public function getCrumbs($separator = '&raquo;', $startText = false) {
if (!empty($this->_crumbs)) {
$out = array();
if ($startText) {
$out[] = $this->link($startText, '/');
if (!is_array($startText)) {
$startText = array(
'url' => '/',
'text' => $startText
);
}
$startText += array('url' => '/', 'text' => __('Home'));
list($url, $text) = array($startText['url'], $startText['text']);
unset($startText['url'], $startText['text']);

$out[] = $this->link($text, $url, $startText);
}

foreach ($this->_crumbs as $crumb) {
Expand Down

0 comments on commit c89c49c

Please sign in to comment.