From c89c49c3107978c14b14bb19d778e0f2e94cb536 Mon Sep 17 00:00:00 2001 From: mark_story Date: Sat, 14 Jan 2012 13:14:26 -0500 Subject: [PATCH] Enhance HtmlHelper::getCrumbs() $startText param It now accepts an array which gives more control and flexibilibity over the first breadcrumb link. Fixes #2475 --- .../Test/Case/View/Helper/HtmlHelperTest.php | 31 +++++++++++++++---- lib/Cake/View/Helper/HtmlHelper.php | 22 +++++++++++-- 2 files changed, 45 insertions(+), 8 deletions(-) diff --git a/lib/Cake/Test/Case/View/Helper/HtmlHelperTest.php b/lib/Cake/Test/Case/View/Helper/HtmlHelperTest.php index a17ce9836c1..e98c15605a0 100644 --- a/lib/Cake/Test/Case/View/Helper/HtmlHelperTest.php +++ b/lib/Cake/Test/Case/View/Helper/HtmlHelperTest.php @@ -866,7 +866,7 @@ public function testCharsetTag() { } /** - * testBreadcrumb method + * testGetCrumb and addCrumb method * * @return void */ @@ -909,11 +909,6 @@ public function testBreadcrumb() { ); $this->assertTags($result, $expected); - $this->assertRegExp('/^]+>First<\/a> > ]+>Second<\/a> > ]+>Third<\/a>$/', $result); - $this->assertRegExp('/]*>First<\/a>/', $result); - $this->assertRegExp('/]*>Second<\/a>/', $result); - $this->assertRegExp('/]*>Third<\/a>/', $result); - $this->assertNotRegExp('/]+[^href]=[^<>]*>/', $result); $this->Html->addCrumb('Fourth', null); @@ -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' => '', '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 * diff --git a/lib/Cake/View/Helper/HtmlHelper.php b/lib/Cake/View/Helper/HtmlHelper.php index 3645b581ed4..41c0a3e74f8 100644 --- a/lib/Cake/View/Helper/HtmlHelper.php +++ b/lib/Cake/View/Helper/HtmlHelper.php @@ -658,8 +658,16 @@ public function style($data, $oneline = true) { /** * Returns the breadcrumb trail as a sequence of »-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 @@ public function getCrumbs($separator = '»', $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) {