Skip to content

Commit

Permalink
Let the category template render the category mini breadcrumb. Item f…
Browse files Browse the repository at this point in the history
…actory now returns an array for the breadcrumb.
  • Loading branch information
romainruaud committed Sep 30, 2016
1 parent bc14c44 commit 14445c9
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,6 @@ class ItemFactory extends \Magento\Search\Model\Autocomplete\ItemFactory
*/
const XML_PATH_CATEGORY_URL_SUFFIX = 'catalog/seo/category_url_suffix';

/**
* The offset to display on the beginning of the Breadcrumb
*/
const START_BREADCRUMB_OFFSET = 1;

/**
* The offset to display on the end of the Breadcrumb
*/
const END_BREADCRUMB_OFFSET = 1;

/**
* The string used when chunking
*/
const CHUNK_STRING = "...";

/**
* @var array An array containing category names, to use as local cache
*/
Expand Down Expand Up @@ -154,29 +139,6 @@ private function getCategoryUrl($category)
* @return array
*/
private function getCategoryBreadcrumb(\Magento\Catalog\Model\Category $category)
{
$chunkPath = $this->getChunkedPath($category);
$breadcrumb = [];

foreach ($chunkPath as $categoryId) {
$breadcrumb[] = $this->getCategoryNameById($categoryId, $category->getStoreId());
}

return implode(' > ', $breadcrumb);
}

/**
* Return chunked (if needed) path for a category
*
* A chunked path is the first 2 highest ancestors and the 2 lowests levels of path
*
* If path is not longer than 4, complete path is used
*
* @param \Magento\Catalog\Model\Category $category The category
*
* @return array
*/
private function getChunkedPath(\Magento\Catalog\Model\Category $category)
{
$path = $category->getPath();
$rawPath = explode('/', $path);
Expand All @@ -187,17 +149,12 @@ private function getChunkedPath(\Magento\Catalog\Model\Category $category)
// Last occurence is the category displayed.
array_pop($rawPath);

$chunkedPath = $rawPath;

if (count($rawPath) > (self::START_BREADCRUMB_OFFSET + self::END_BREADCRUMB_OFFSET)) {
$chunkedPath = array_merge(
array_slice($rawPath, 0, self::START_BREADCRUMB_OFFSET),
[self::CHUNK_STRING],
array_slice($rawPath, -self::END_BREADCRUMB_OFFSET)
);
$breadcrumb = [];
foreach ($rawPath as $categoryId) {
$breadcrumb[] = $this->getCategoryNameById($categoryId, $category->getStoreId());
}

return $chunkedPath;
return $breadcrumb;
}

/**
Expand All @@ -210,10 +167,6 @@ private function getChunkedPath(\Magento\Catalog\Model\Category $category)
*/
private function getCategoryNameById($categoryId, $storeId)
{
if ($categoryId == self::CHUNK_STRING) {
return self::CHUNK_STRING;
}

if (!isset($this->categoryNames[$categoryId])) {
$categoryResource = $this->categoryResource;
$this->categoryNames[$categoryId] = $categoryResource->getAttributeRawValue($categoryId, "name", $storeId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
*/
-->
<dd class="<%- data.row_class %>" id="qs-option-<%- data.index %>" role="option" href="<%- data.url %>">
<span class="category-mini-crumb"><%- data.breadcrumb %></span>
<span class="category-mini-crumb"><%- data.breadcrumb.join(' > ') %></span>
<span class="qs-option-name"><%- data.title %></span>
<span aria-hidden="true" class="amount"><%- data.num_results %></span>
</dd>

0 comments on commit 14445c9

Please sign in to comment.