Enhance HtmlHelper::getCrumbs() $startText param

It now accepts an array which gives more control and flexibilibity
over the first breadcrumb link.

Fixes #2475
This commit is contained in:
mark_story 2012-01-14 13:14:26 -05:00
parent 7badb1d252
commit c89c49c310
2 changed files with 45 additions and 8 deletions

View file

@ -866,7 +866,7 @@ class HtmlHelperTest extends CakeTestCase {
}
/**
* testBreadcrumb method
* testGetCrumb and addCrumb method
*
* @return void
*/
@ -909,11 +909,6 @@ class HtmlHelperTest extends CakeTestCase {
);
$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);
@ -958,6 +953,30 @@ class HtmlHelperTest extends CakeTestCase {
$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
*

View file

@ -658,8 +658,16 @@ class HtmlHelper extends AppHelper {
/**
* 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
*/
@ -667,7 +675,17 @@ class HtmlHelper extends AppHelper {
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) {