Skip to content

Commit

Permalink
ref: Use the default breadcrumbs template that is used in theme-creat…
Browse files Browse the repository at this point in the history
…iveshop ; show breadcrumbs for all categories ancestors, not just the final child (fixes EaDesgin#29)
  • Loading branch information
jtomaszewski committed Feb 6, 2021
1 parent a6ee418 commit cfe578b
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 179 deletions.
85 changes: 43 additions & 42 deletions Block/FullBreadcrumbs.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,71 +46,72 @@ public function __construct(
parent::__construct($context, $data);
}

public function getExcludedCategoriesIds()
private function getExcludedCategoriesIds()
{
$excluded_categories_ids = $this->breadcrumbsData->hasConfig('full_breadcrumbs/general/excluded_categories_ids');
return explode(',', str_replace(' ', '', $excluded_categories_ids));
}

public function isEnabled()
private function isEnabled()
{
return $this->breadcrumbsData->hasConfig('full_breadcrumbs/general/enabled');
}

public function getProduct()
private function getCurrentProduct()
{
return $this->registry->registry('current_product');
}

public function getCategoryProductIds($product)
private function getProductCategories()
{
/** @var $categoryIds AttributeValue */
$categoryIds = $product->getCategoryIds();
return $categoryIds;
}

public function getFilteredCollection($categoryIds)
{
$collection = $this->categoryCollection->create();
$filtered_colection = $collection
->addFieldToSelect('*')
->addFieldToFilter(
'entity_id',
['in' => $categoryIds]
)
->setOrder('level', 'ASC')
->load();
return $filtered_colection;
}
$productCategories = $this->getCurrentProduct()->getCategoryCollection();
if (count($productCategories) === 0) {
return [];
}

public function getCategories($filtered_colection, $badCategories)
{
$separator = ' <span class="breadcrumbsseparator"></span> ';
$categories = '';
foreach ($filtered_colection as $categoriesData) {
if (!in_array($categoriesData->getId(), $badCategories)) {
$categories .= '<a href="' . $categoriesData->getUrl() . '">';
$categories .= $categoriesData->getData('name') . '</a>' . $separator;
$productCategory = current(iterator_to_array($productCategories));
$categories = $productCategory->getParentCategories();
$excludedCategoriesIds = $this->getExcludedCategoriesIds();
$filteredCategories = array_filter(
$categories,
function ($category) use($excludedCategoriesIds) {
return !in_array($category->getId(), $excludedCategoriesIds);
}
}
return $categories;
);
return $filteredCategories;
}

public function getProductBreadcrumbs()
{
if ($this->isEnabled()) {
$separator = ' <span class="breadcrumbsseparator"></span> ';
$product = $this->getProduct();
$categoryIds = $this->getCategoryProductIds($product);
if (!$this->isEnabled()) {
return [];
}

$filtered_colection = $this->getFilteredCollection($categoryIds);
$breadcrumbs = [];
$categories = $this->getProductCategories();

$badCategories = $this->getExcludedCategoriesIds();
$breadcrumbs['home'] = [
'link' => $this->_storeManager->getStore()->getBaseUrl(),
'label' => __('Home'),
'title' => null,
'last' => false
];
foreach ($categories as $category) {
$breadcrumbs[ 'category' . $category->getId() ] = [
'link' => $category->getUrl(),
'label' => $category->getData('name'),
'title' => null,
'last' => false
];
}

$categories = $this->getCategories($filtered_colection, $badCategories);
$breadcrumbs['product'] = [
'link' => null,
'label' => $this->getCurrentProduct()->getName(),
'title' => null,
'last' => true
];

$home_url = '<a href="' . $this->_storeManager->getStore()->getBaseUrl() . '">Home</a>';
return $home_url . $separator . $categories . '<span>' . $product->getName() . '</span>';
}
return $breadcrumbs;
}
}
6 changes: 3 additions & 3 deletions Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

class Data extends AbstractHelper
{
const ENABLED = 'groomershop_full_breadcrumbs/fullbreadcrumbs/enabled';
const ENABLED = 'full_breadcrumbs/general/enabled';

public $config;

Expand All @@ -38,8 +38,8 @@ public function hasConfig($configPath)
);
}

public function isEnabled($enablefullbreadcrumbs = self::ENABLED)
public function isEnabled()
{
return $this->hasConfig($enablefullbreadcrumbs);
return $this->hasConfig(self::ENABLED);
}
}
48 changes: 0 additions & 48 deletions Test/Unit/Block/FullBreadcrumbsTest.php

This file was deleted.

73 changes: 0 additions & 73 deletions Test/Unit/Helper/DataTest.php

This file was deleted.

7 changes: 3 additions & 4 deletions view/frontend/layout/catalog_product_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@
-->
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceBlock name="page.main.title">
<block as="fullbreadcrumbs" class="Groomershop\FullBreadcrumbs\Block\FullBreadcrumbs" name="fullbreadcrumbs" template="Groomershop_FullBreadcrumbs::fullbreadcrumbs.phtml"/>
</referenceBlock>
<move element="fullbreadcrumbs" destination="page.main.title" before="page.main.title" />
<block name="full_breadcrumbs" class="Groomershop\FullBreadcrumbs\Block\FullBreadcrumbs" template="Groomershop_FullBreadcrumbs::breadcrumbs.phtml" />
<move element="full_breadcrumbs" destination="breadcrumbs.container.inner"/>
<referenceBlock name="breadcrumbs" remove="true" />
</body>
</page>
46 changes: 46 additions & 0 deletions view/frontend/templates/breadcrumbs.phtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@

<?php
/**
* @var $block \Groomershop\FullBreadcrumbs\Block\FullBreadcrumbs
*/

$crumbs = $block->getProductBreadcrumbs();
if (count($crumbs) > 0) : ?>
<?php $displayCustomSeparator = $block->getVar('breadcrumbs/custom_separator/display', 'Magento_Theme'); ?>
<div class="cs-breadcrumbs">
<ul class="cs-breadcrumbs__list">
<?php $i = 1; foreach ($crumbs as $crumbName => $crumbInfo) : ?>
<li class="cs-breadcrumbs__item <?php /* @escapeNotVerified */ echo $crumbName ?>">
<?php if ($crumbInfo['link']) : ?>
<a
class="cs-breadcrumbs__link"
href="<?php /* @escapeNotVerified */ echo $crumbInfo['link'] ?>"
title="<?php echo $block->escapeHtml($crumbInfo['title']) ?>"
>
<span>
<?php echo $block->escapeHtml($crumbInfo['label']) ?>
</span>
</a>
<?php if ($displayCustomSeparator) : ?>
<span class="cs-breadcrumbs__separator">
<?= $this->getLayout()
->createBlock('MageSuite\ThemeHelpers\Block\Icon')
->setIconUrl($block->getVar('breadcrumbs/custom_separator/icon'))
->setCssClass('cs-breadcrumbs__separator-icon')
->setInlined(true)
->toHtml();
?>
</span>
<?php endif; ?>
<?php /* elseif ($crumbInfo['last']) : ?>
<strong><?php echo $block->escapeHtml($crumbInfo['label']) ?></strong>
<?php */ else: ?>
<span>
<?php echo $block->escapeHtml($crumbInfo['label']) ?>
</span>
<?php endif; ?>
</li>
<?php $i++; endforeach; ?>
</ul>
</div>
<?php endif; ?>
9 changes: 0 additions & 9 deletions view/frontend/templates/fullbreadcrumbs.phtml

This file was deleted.

0 comments on commit cfe578b

Please sign in to comment.