Commits

Sam Stenvall committed 5c7f9c9

refactored normalizeItems() to get rid of duplicate code

Comments (0)

Files changed (3)

widgets/BootBaseMenu.php

 	}
 
 	/**
+	 * Normalizes the items in this menu.
+	 * @param array $items the items to be normalized
+	 * @param string $route the route of the current request
+	 * @return array the normalized menu items
+	 */
+	protected function normalizeItems($items, $route)
+	{
+		foreach ($items as $i => $item)
+		{
+			if (!is_array($item))
+				continue;
+
+			if (isset($item['visible']) && !$item['visible'])
+			{
+				unset($items[$i]);
+				continue;
+			}
+			
+			if (!isset($item['label']))
+				$item['label'] = '';
+			
+			if (isset($item['encodeLabel']))
+				$items[$i]['encodeLabel'] = $item['encodeLabel'];
+			else
+				$items[$i]['encodeLabel'] = $this->encodeLabel;
+
+			if (!isset($item['active']))
+				$items[$i]['active'] = $this->isItemActive($item, $route);
+		}
+		
+		return array_values($items);
+	}
+	
+	/**
 	 * Renders a single item in the menu.
 	 * @param array $item the item configuration
 	 * @return string the rendered item

widgets/BootDropdown.php

 		}
 	}
 
-	/**
-	 * Normalizes the items in this menu.
-	 * @param array $items the items to be normalized
-	 * @param string $route the route of the current request
-	 * @return array the normalized menu items
-	 */
-	protected function normalizeItems($items, $route)
-	{
-		foreach ($items as $i => $item)
-		{
-			if (!is_array($item))
-				continue;
-
-			if (isset($item['visible']) && !$item['visible'])
-			{
-				unset($items[$i]);
-				continue;
-			}
-
-			if (!is_array($item)) {
-				continue;
-			}
-
-			if (!isset($item['label']))
-				$item['label'] = '';
-
-			if (isset($item['encodeLabel']))
-				$items[$i]['encodeLabel'] = $item['encodeLabel'];
-			else
-				$items[$i]['encodeLabel'] = $this->encodeLabel;
-
-			if (!isset($item['active']))
-				$items[$i]['active'] = $this->isItemActive($item, $route);
-		}
-
-		return array_values($items);
-	}
 }

widgets/BootMenu.php

 	 */
 	protected function normalizeItems($items, $route)
 	{
+		$items = parent::normalizeItems($items, $route);
+		
+		// Recurse through submenus and normalize them as well
 		foreach ($items as $i => $item)
 		{
-			if (!is_array($item))
-				continue;
-
-			if (isset($item['visible']) && !$item['visible'])
-			{
-				unset($items[$i]);
-				continue;
-			}
-
-			if (!isset($item['label']))
-				$item['label'] = '';
-
-			if (isset($item['encodeLabel']))
-				$items[$i]['encodeLabel'] = $item['encodeLabel'];
-			else
-				$items[$i]['encodeLabel'] = $this->encodeLabel;
-			
 			if (!empty($item['items']) && is_array($item['items']))
 			{
 				$items[$i]['items'] = $this->normalizeItems($item['items'], $route);
 				if (empty($items[$i]['items']))
 					unset($items[$i]['items']);
 			}
-
-			if (!isset($item['active']))
-				$items[$i]['active'] = $this->isItemActive($item, $route);
 		}
 
 		return array_values($items);