Skip to content

Commit

Permalink
Multi layer navigation added
Browse files Browse the repository at this point in the history
  • Loading branch information
Silvan Laroo committed Apr 6, 2016
1 parent 4d4c86e commit fb5f00f
Show file tree
Hide file tree
Showing 8 changed files with 296 additions and 67 deletions.
189 changes: 147 additions & 42 deletions Block/Sidebar.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php namespace Sebwite\Sidebar\Block;
<?php namespace Sebwite\Sidebar\Block;

use Magento\Catalog\Model\ResourceModel\Eav\Attribute;
use Magento\Catalog\Model\ResourceModel\Product;
use Magento\Framework\View\Element\Template;

/**
Expand All @@ -10,7 +12,8 @@
* @package Sebwite\Sidebar
* @copyright Copyright (c) 2015, Sebwite. All rights reserved
*/
class Sidebar extends Template {
class Sidebar extends Template
{

/**
* @var \Magento\Catalog\Helper\Category
Expand All @@ -26,19 +29,29 @@ class Sidebar extends Template {
* @var \Magento\Catalog\Model\Indexer\Category\Flat\State
*/
protected $categoryFlatConfig;

/**
* @var \Magento\Catalog\Model\CategoryFactory
*/
private $_categoryFactory;
protected $_categoryFactory;

/** @var \Magento\Catalog\Model\ResourceModel\Product\Collection */
protected $_productCollectionFactory;

/** @var \Magento\Catalog\Helper\Output */
private $helper;

/**
* @param Template\Context $context
* @param \Magento\Catalog\Helper\Category $categoryHelper
* @param \Magento\Framework\Registry $registry
* @param \Magento\Catalog\Model\Indexer\Category\Flat\State $categoryFlatState
* @param \Magento\Catalog\Model\CategoryFactory $categoryFactory
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
* @param array $data
* @param Template\Context $context
* @param \Magento\Catalog\Helper\Category $categoryHelper
* @param \Magento\Framework\Registry $registry
* @param \Magento\Catalog\Model\Indexer\Category\Flat\State $categoryFlatState
* @param \Magento\Catalog\Model\CategoryFactory $categoryFactory
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
* @param \Magento\Catalog\Model\ResourceModel\Product\Collection $productCollectionFactory
* @param \Magento\Framework\App\ObjectManager $objectManager
*
* @internal param array $data
*/
public function __construct(
\Magento\Framework\View\Element\Template\Context $context,
Expand All @@ -47,12 +60,17 @@ public function __construct(
\Magento\Catalog\Model\Indexer\Category\Flat\State $categoryFlatState,
\Magento\Catalog\Model\CategoryFactory $categoryFactory,
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
$data = []
) {
$this->_categoryHelper = $categoryHelper;
$this->_coreRegistry = $registry;
$this->categoryFlatConfig = $categoryFlatState;
$this->_categoryFactory = $categoryFactory;
\Magento\Catalog\Model\ResourceModel\Product\Collection $productCollectionFactory,
\Magento\Catalog\Helper\Output $helper,
$data = [ ]
)
{
$this->_categoryHelper = $categoryHelper;
$this->_coreRegistry = $registry;
$this->categoryFlatConfig = $categoryFlatState;
$this->_categoryFactory = $categoryFactory;
$this->_productCollectionFactory = $productCollectionFactory;
$this->helper = $helper;

parent::__construct($context, $data);
}
Expand All @@ -62,27 +80,6 @@ public function __construct(
* @return string
*/

/**
* Get current category
*
* @param $category
* @return Category
*/
public function isActive($category)
{
$activeCategory = $this->_coreRegistry->registry('current_category');

if( ! $activeCategory)
return false;

// Check if this is the active category
if ($this->categoryFlatConfig->isFlatEnabled() && $category->getUseFlatResource())
return (($category->getId() == $activeCategory->getId()) ? true : false);

// Fallback - If Flat categories is not enabled the active category does not give an id
return (($category->getName() == $activeCategory->getName()) ? true : false);
}

/**
* Get all categories
*
Expand All @@ -95,33 +92,95 @@ public function isActive($category)
public function getCategories($sorted = false, $asCollection = false, $toLoad = true)
{
$cacheKey = sprintf('%d-%d-%d-%d', $this->getSelectedRootCategory(), $sorted, $asCollection, $toLoad);
if (isset($this->_storeCategories[$cacheKey])) {
return $this->_storeCategories[$cacheKey];
if ( isset($this->_storeCategories[ $cacheKey ]) )
{
return $this->_storeCategories[ $cacheKey ];
}

/**
* Check if parent node of the store still exists
*/
$category = $this->_categoryFactory->create();

$storeCategories = $category->getCategories($this->getSelectedRootCategory(), $recursionLevel = 1, $sorted, $asCollection, $toLoad);

$this->_storeCategories[$cacheKey] = $storeCategories;
$this->_storeCategories[ $cacheKey ] = $storeCategories;

return $storeCategories;
}

/**
* getSelectedRootCategory method
*
* @return int|mixed
*/
public function getSelectedRootCategory()
{
$category = $this->_scopeConfig->getValue(
'sebwite_sidebar/general/category'
);

if($category === null)
if ( $category === null )
{
return 1;
}

return $category;
}

/**
* @param $category
* @param string $html
* @param int $level
*
* @return string
*/
public function getChildCategoryView($category, $html = '', $level = 1)
{
// Check if category has children
if ( $category->hasChildren() )
{

$childCategories = $this->getSubcategories($category);

if ( count($childCategories) > 0 )
{

$html .= '<ul class="o-list o-list--unstyled">';

// Loop through children categories
foreach ( $childCategories as $childCategory )
{

$html .= '<li class="level' . $level . ($this->isActive($childCategory) ? ' active' : '') . '">';
$html .= '<a href="' . $this->getCategoryUrl($childCategory) . '" title="' . $childCategory->getName() . '" class="' . ($this->isActive($childCategory) ? 'is-active' : '') . '">' . $childCategory->getName() . '</a>';

if ( $childCategory->hasChildren() )
{
if ( $this->isActive($childCategory) )
{
$html .= '<span class="expanded"><i class="fa fa-minus"></i></span>';
}
else
{
$html .= '<span class="expand"><i class="fa fa-plus"></i></span>';
}
}

if ( $childCategory->hasChildren() )
{
$html .= $this->getChildCategoryView($childCategory, '', ($level + 1));
}

$html .= '</li>';
}
$html .= '</ul>';
}
}

return $html;
}

/**
* Retrieve subcategories
*
Expand All @@ -131,16 +190,62 @@ public function getSelectedRootCategory()
*/
public function getSubcategories($category)
{
if ($this->categoryFlatConfig->isFlatEnabled() && $category->getUseFlatResource())
if ( $this->categoryFlatConfig->isFlatEnabled() && $category->getUseFlatResource() )
{
return (array)$category->getChildrenNodes();
}

return $category->getChildren();
}

/**
* Get current category
*
* @param \Magento\Catalog\Model\Category $category
*
* @return Category
*/
public function isActive($category)
{
$activeCategory = $this->_coreRegistry->registry('current_category');
$activeProduct = $this->_coreRegistry->registry('current_product');

if ( !$activeCategory )
{

// Check if we're on a product page
if ( $activeProduct !== null )
{
return in_array($category->getId(), $activeProduct->getCategoryIds());
}

return false;
}

// Check if this is the active category
if ( $this->categoryFlatConfig->isFlatEnabled() && $category->getUseFlatResource() AND
$category->getId() == $activeCategory->getId()
)
{
return true;
}

// Check if a subcategory of this category is active
$childrenIds = $category->getAllChildren(true);
if ( !is_null($childrenIds) AND in_array($activeCategory->getId(), $childrenIds) )
{
return true;
}

// Fallback - If Flat categories is not enabled the active category does not give an id
return (($category->getName() == $activeCategory->getName()) ? true : false);
}

/**
* Return Category Id for $category object
*
* @param $category
*
* @return string
*/
public function getCategoryUrl($category)
Expand Down
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Magento 2.0 Category Sidebar extension
![Alt text](header.jpg?raw=true "Magento2 Category sidebar")
This extension will add the ability to show one of your root categories in a sidebar. The root category can be selected from the Magento2 admin config page.

## Installation with composer
Expand All @@ -15,6 +16,9 @@ This extension will add the ability to show one of your root categories in a sid
* Clear cache

## Configuration
* Select the root category you want to use from the config page
* Select the root category you want to use from the config page from the admin panel
* You should implement the block `Sebwite\Sidebar\Block\Sidebar` in your theme to make this extension work. Example
`<block class="Sebwite\Sidebar\Block\Sidebar" name="category-sidebar" template="Sebwite_Sidebar::sidebar.phtml" />`
`<block class="Sebwite\Sidebar\Block\Sidebar" name="category-sidebar" template="Sebwite_Sidebar::sidebar.phtml" />`

---
[![Alt text](https://www.sebwite.nl/wp-content/themes/sebwite/assets/images/logo-sebwite.png "Sebwite.nl")](https://sebwite.nl)
Binary file added header.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
15 changes: 15 additions & 0 deletions view/frontend/layout/default.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0"?>
<!--
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<head>
<script type="text/javascript" src="Sebwite_Sidebar::js/module.js"/>
<css src="Sebwite_Sidebar::css/module.css" />
<css src="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css" src_type="url" />
</head>
</page>
16 changes: 16 additions & 0 deletions view/frontend/page_layout/2columns-left.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0"?>
<!--
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<layout xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_layout.xsd">
<update handle="2columns-left"/>

<referenceContainer name="columns">
<container name="div.sidebar.main" htmlTag="div" htmlClass="c-sidebar c-sidebar--categories">
<block class="Sebwite\Sidebar\Block\Sidebar" name="category-sidebar" template="Sebwite_Sidebar::sidebar.phtml"/>
</container>
</referenceContainer>
</layout>
42 changes: 19 additions & 23 deletions view/frontend/templates/sidebar.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,25 @@
* @var $block Sebwite\Sidebar\Block\Sidebar
*/
$categories = $block->getCategories();
$brands = $block->getBrands();
?>

// Loop through categories
foreach($categories as $category) {
<h3><?php echo __('Categories'); ?></h3>
<ul class="o-list">

// Echo parent category
echo '<h4><a href="' . $block->getCategoryUrl($category) . '" title="' . $category->getName() . '">' . $category->getName() . '</a></h4>';
<?php
// Loop through categories
foreach ( $categories as $category ) :
?>
<li class="level0<?php echo($block->isActive($category) ? ' active' : ''); ?>">
<a href="<?php echo $block->getCategoryUrl($category); ?>" title="<?php echo $category->getName(); ?>" class="<?php echo($block->isActive($category) ? ' active' : ''); ?>">
<?php echo $category->getName(); ?>
</a>
<?php if ( $category->hasChildren() ) : ?>
<span class="expand"><?php echo $block->isActive($category) ? '<i class="fa fa-minus"></i>' : '<i class="fa fa-plus"></i>'; ?></span>
<?php endif; ?>

// Check if category has children
if($category->hasChildren()) {

$childCategories = $block->getSubcategories($category);

if(count($childCategories) > 0) {

echo '<ul class="o-list o-list--unstyled">';
// Loop through children categories
foreach($childCategories as $childCategory) {

echo '<li ' . (($block->isActive($childCategory) == true) ? 'class="is-active"' : '') . ' >';
echo '<a href="' . $block->getCategoryUrl($childCategory) . '" title="' . $childCategory->getName() . '">' . $childCategory->getName() . '</a>';
echo '</li>';
}
echo '</ul>';
}
}
}
<?php echo $block->getChildCategoryView($category); ?>
</li>
<?php endforeach; ?>
</ul>
Loading

0 comments on commit fb5f00f

Please sign in to comment.