Skip to content
This repository
Browse code

Keeping code dry in HtmlHelper::getCrumbs/getCrumbList

  • Loading branch information...
commit 95ba5f45a5553d514b9ab42914b5ce00a485b946 1 parent f138c73
Tigran Gabrielyan authored February 21, 2012

Showing 1 changed file with 26 additions and 28 deletions. Show diff stats Hide diff stats

  1. 54  lib/Cake/View/Helper/HtmlHelper.php
54  lib/Cake/View/Helper/HtmlHelper.php
@@ -684,21 +684,8 @@ public function style($data, $oneline = true) {
684 684
 	public function getCrumbs($separator = '»', $startText = false) {
685 685
 		if (!empty($this->_crumbs)) {
686 686
 			$out = array();
687  
-			if ($startText) {
688  
-				if (!is_array($startText)) {
689  
-					$startText = array(
690  
-						'url' => '/',
691  
-						'text' => $startText
692  
-					);
693  
-				}
694  
-				$startText += array('url' => '/', 'text' => __('Home'));
695  
-				list($url, $text) = array($startText['url'], $startText['text']);
696  
-				unset($startText['url'], $startText['text']);
697  
-
698  
-				$out[] = $this->link($text, $url, $startText);
699  
-			}
700  
-
701  
-			foreach ($this->_crumbs as $crumb) {
  687
+			$crumbs = $this->_prepareCrumbs($startText);
  688
+			foreach ($crumbs as $crumb) {
702 689
 				if (!empty($crumb[1])) {
703 690
 					$out[] = $this->link($crumb[0], $crumb[1], $crumb[2]);
704 691
 				} else {
@@ -727,19 +714,7 @@ public function getCrumbs($separator = '»', $startText = false) {
727 714
 	public function getCrumbList($options = array(), $startText = false) {
728 715
 		if (!empty($this->_crumbs)) {
729 716
 			$result = '';
730  
-			$crumbs = $this->_crumbs;
731  
-			if ($startText) {
732  
-				if (!is_array($startText)) {
733  
-					$startText = array(
734  
-						'url' => '/',
735  
-						'text' => $startText
736  
-					);
737  
-				}
738  
-				$startText += array('url' => '/', 'text' => __('Home'));
739  
-				list($url, $text) = array($startText['url'], $startText['text']);
740  
-				unset($startText['url'], $startText['text']);
741  
-				array_unshift($crumbs, array($text, $url, $startText));
742  
-			}
  717
+			$crumbs = $this->_prepareCrumbs($startText);
743 718
 			$crumbCount = count($crumbs);
744 719
 			$ulOptions = $options;
745 720
 			foreach ($crumbs as $which => $crumb) {
@@ -763,6 +738,29 @@ public function getCrumbList($options = array(), $startText = false) {
763 738
 	}
764 739
 
765 740
 /**
  741
+ * Prepends startText to crumbs array if set
  742
+ *
  743
+ * @param $startText
  744
+ * @return array Crumb list including startText (if provided)
  745
+ */
  746
+	protected function _prepareCrumbs($startText) {
  747
+		$crumbs = $this->_crumbs;
  748
+		if ($startText) {
  749
+			if (!is_array($startText)) {
  750
+				$startText = array(
  751
+					'url' => '/',
  752
+					'text' => $startText
  753
+				);
  754
+			}
  755
+			$startText += array('url' => '/', 'text' => __('Home'));
  756
+			list($url, $text) = array($startText['url'], $startText['text']);
  757
+			unset($startText['url'], $startText['text']);
  758
+			array_unshift($crumbs, array($text, $url, $startText));
  759
+		}
  760
+		return $crumbs;
  761
+	}
  762
+
  763
+/**
766 764
  * Creates a formatted IMG element.
767 765
  *
768 766
  * This method will set an empty alt attribute if one is not supplied.

0 notes on commit 95ba5f4

Please sign in to comment.
Something went wrong with that request. Please try again.