Skip to content

Commit

Permalink
GraphQL-19: Add breadcrumbs support
Browse files Browse the repository at this point in the history
  • Loading branch information
Valeriy Nayda committed Jul 19, 2018
1 parent 3d973af commit cfd7502
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,77 +7,56 @@

namespace Magento\CatalogGraphQl\Model\Resolver\Category;

use \Magento\CatalogGraphQl\Model\Resolver\Category\DataProvider\Breadcrumbs as BreadcrumbsDataProvider;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Query\Resolver\Value;
use Magento\Framework\GraphQl\Query\Resolver\ValueFactory;
use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory;

/**
* Retrieves breadcrumbs
*/
class Breadcrumbs implements ResolverInterface
{
/**
* @var CollectionFactory
* @var BreadcrumbsDataProvider
*/
private $collectionFactory;
private $breadcrumbsDataProvider;

/**
* @var ValueFactory
*/
private $valueFactory;

/**
* @param BreadcrumbsDataProvider $breadcrumbsDataProvider
* @param ValueFactory $valueFactory
* @param CollectionFactory $collectionFactory
*/
public function __construct(
ValueFactory $valueFactory,
CollectionFactory $collectionFactory
BreadcrumbsDataProvider $breadcrumbsDataProvider,
ValueFactory $valueFactory
) {
$this->collectionFactory = $collectionFactory;
$this->breadcrumbsDataProvider = $breadcrumbsDataProvider;
$this->valueFactory = $valueFactory;
}

/**
* {@inheritdoc}
* @inheritdoc
*/
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null): Value
{
$breadcrumbs = [];

if (!isset($value['path'])) {
$result = function () {
return null;
};
return $this->valueFactory->create($result);
}

$categoryPath = $value['path'];
$pathCategoryIds = explode('/', $categoryPath);
$parentCategoryIds = array_slice($pathCategoryIds, 2, count($pathCategoryIds) - 3);

if (count($parentCategoryIds)) {
$collection = $this->collectionFactory->create();
$collection->addAttributeToSelect(['name', 'url_key']);
$collection->addAttributeToFilter('entity_id', $parentCategoryIds);

foreach ($collection as $category) {
$breadcrumbs[] = [
'category_id' => $category->getId(),
'category_name' => $category->getName(),
'category_level' => $category->getLevel(),
'category_url_key' => $category->getUrlKey(),
];
}
}

$result = function () use ($breadcrumbs) {
return count($breadcrumbs) ? $breadcrumbs : null;
$result = function () use ($value) {
$breadcrumbsData = $this->breadcrumbsDataProvider->getData($value['path']);
return count($breadcrumbsData) ? $breadcrumbsData : null;
};

return $this->valueFactory->create($result);
}
}
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;
}
}

0 comments on commit cfd7502

Please sign in to comment.