-
Notifications
You must be signed in to change notification settings - Fork 9.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Valeriy Nayda
committed
Jul 19, 2018
1 parent
3d973af
commit cfd7502
Showing
2 changed files
with
69 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
app/code/Magento/CatalogGraphQl/Model/Resolver/Category/DataProvider/Breadcrumbs.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\CatalogGraphQl\Model\Resolver\Category\DataProvider; | ||
|
||
use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory; | ||
|
||
/** | ||
* Breadcrumbs data provider | ||
*/ | ||
class Breadcrumbs | ||
{ | ||
/** | ||
* @var CollectionFactory | ||
*/ | ||
private $collectionFactory; | ||
|
||
/** | ||
* @param CollectionFactory $collectionFactory | ||
*/ | ||
public function __construct( | ||
CollectionFactory $collectionFactory | ||
) { | ||
$this->collectionFactory = $collectionFactory; | ||
} | ||
|
||
/** | ||
* @param string $categoryPath | ||
* @return array | ||
*/ | ||
public function getData(string $categoryPath): array | ||
{ | ||
$breadcrumbsData = []; | ||
|
||
$pathCategoryIds = explode('/', $categoryPath); | ||
$parentCategoryIds = array_slice($pathCategoryIds, 2, -1); | ||
|
||
if (count($parentCategoryIds)) { | ||
$collection = $this->collectionFactory->create(); | ||
$collection->addAttributeToSelect(['name', 'url_key']); | ||
$collection->addAttributeToFilter('entity_id', $parentCategoryIds); | ||
|
||
foreach ($collection as $category) { | ||
$breadcrumbsData[] = [ | ||
'category_id' => $category->getId(), | ||
'category_name' => $category->getName(), | ||
'category_level' => $category->getLevel(), | ||
'category_url_key' => $category->getUrlKey(), | ||
]; | ||
} | ||
} | ||
return $breadcrumbsData; | ||
} | ||
} |