From af2fd0359072eebccc68f5bbc8e7ddfd6c1af829 Mon Sep 17 00:00:00 2001 From: Tigran Gabrielyan Date: Thu, 16 Feb 2012 18:27:41 -0800 Subject: [PATCH] Added `startText` feature to HtmlHelper::getCrumbList() --- .../Test/Case/View/Helper/HtmlHelperTest.php | 44 +++++++++++++++++++ lib/Cake/View/Helper/HtmlHelper.php | 21 +++++++-- 2 files changed, 62 insertions(+), 3 deletions(-) diff --git a/lib/Cake/Test/Case/View/Helper/HtmlHelperTest.php b/lib/Cake/Test/Case/View/Helper/HtmlHelperTest.php index 1d43e624d..39ee9b46a 100644 --- a/lib/Cake/Test/Case/View/Helper/HtmlHelperTest.php +++ b/lib/Cake/Test/Case/View/Helper/HtmlHelperTest.php @@ -1785,6 +1785,50 @@ class HtmlHelperTest extends CakeTestCase { ); } +/** + * Test getCrumbList startText + */ + public function testCrumbListFirstLink() { + $this->Html->addCrumb('First', '#first'); + $this->Html->addCrumb('Second', '#second'); + + $result = $this->Html->getCrumbList(null, 'Home'); + $this->assertTags( + $result, + array( + ' array('class' => 'first')), + array('a' => array('href' => '/')), 'Home', '/a', + '/li', + ' array('href' => '#first')), 'First', '/a', + '/li', + array('li' => array('class' => 'last')), + array('a' => array('href' => '#second')), 'Second', '/a', + '/li', + '/ul' + ) + ); + + $result = $this->Html->getCrumbList(null, array('url' => '/home', 'text' => '', 'escape' => false)); + $this->assertTags( + $result, + array( + ' array('class' => 'first')), + array('a' => array('href' => '/home')), 'img' => array('src' => '/home.png'), '/a', + '/li', + ' array('href' => '#first')), 'First', '/a', + '/li', + array('li' => array('class' => 'last')), + array('a' => array('href' => '#second')), 'Second', '/a', + '/li', + '/ul' + ) + ); + } + /** * testLoadConfig method * diff --git a/lib/Cake/View/Helper/HtmlHelper.php b/lib/Cake/View/Helper/HtmlHelper.php index b83079222..4c002cb19 100644 --- a/lib/Cake/View/Helper/HtmlHelper.php +++ b/lib/Cake/View/Helper/HtmlHelper.php @@ -719,15 +719,30 @@ class HtmlHelper extends AppHelper { * crumb was added with. * * @param array $options Array of html attributes to apply to the generated list elements. + * @param mixed $startText This will be the first crumb, if false it defaults to first crumb in array. Can + * also be an array, see `HtmlHelper::getCrumbs` for details. * @return string breadcrumbs html list * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/html.html#creating-breadcrumb-trails-with-htmlhelper */ - public function getCrumbList($options = array()) { + public function getCrumbList($options = array(), $startText = false) { if (!empty($this->_crumbs)) { $result = ''; - $crumbCount = count($this->_crumbs); + $crumbs = $this->_crumbs; + if ($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']); + array_unshift($crumbs, array($text, $url, $startText)); + } + $crumbCount = count($crumbs); $ulOptions = $options; - foreach ($this->_crumbs as $which => $crumb) { + foreach ($crumbs as $which => $crumb) { $options = array(); if (empty($crumb[1])) { $elementContent = $crumb[0];