Skip to content
This repository was archived by the owner on Apr 29, 2019. It is now read-only.

Commit cfd7502

Browse files
author
Valeriy Nayda
committed
GraphQL-19: Add breadcrumbs support
1 parent 3d973af commit cfd7502

File tree

2 files changed

+69
-32
lines changed

2 files changed

+69
-32
lines changed

app/code/Magento/CatalogGraphQl/Model/Resolver/Category/Breadcrumbs.php

+11-32
Original file line numberDiff line numberDiff line change
@@ -7,77 +7,56 @@
77

88
namespace Magento\CatalogGraphQl\Model\Resolver\Category;
99

10+
use \Magento\CatalogGraphQl\Model\Resolver\Category\DataProvider\Breadcrumbs as BreadcrumbsDataProvider;
1011
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
1112
use Magento\Framework\GraphQl\Config\Element\Field;
1213
use Magento\Framework\GraphQl\Query\ResolverInterface;
1314
use Magento\Framework\GraphQl\Query\Resolver\Value;
1415
use Magento\Framework\GraphQl\Query\Resolver\ValueFactory;
15-
use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory;
1616

1717
/**
1818
* Retrieves breadcrumbs
1919
*/
2020
class Breadcrumbs implements ResolverInterface
2121
{
2222
/**
23-
* @var CollectionFactory
23+
* @var BreadcrumbsDataProvider
2424
*/
25-
private $collectionFactory;
25+
private $breadcrumbsDataProvider;
2626

2727
/**
2828
* @var ValueFactory
2929
*/
3030
private $valueFactory;
3131

3232
/**
33+
* @param BreadcrumbsDataProvider $breadcrumbsDataProvider
3334
* @param ValueFactory $valueFactory
34-
* @param CollectionFactory $collectionFactory
3535
*/
3636
public function __construct(
37-
ValueFactory $valueFactory,
38-
CollectionFactory $collectionFactory
37+
BreadcrumbsDataProvider $breadcrumbsDataProvider,
38+
ValueFactory $valueFactory
3939
) {
40-
$this->collectionFactory = $collectionFactory;
40+
$this->breadcrumbsDataProvider = $breadcrumbsDataProvider;
4141
$this->valueFactory = $valueFactory;
4242
}
4343

4444
/**
45-
* {@inheritdoc}
45+
* @inheritdoc
4646
*/
4747
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null): Value
4848
{
49-
$breadcrumbs = [];
50-
5149
if (!isset($value['path'])) {
5250
$result = function () {
5351
return null;
5452
};
5553
return $this->valueFactory->create($result);
5654
}
5755

58-
$categoryPath = $value['path'];
59-
$pathCategoryIds = explode('/', $categoryPath);
60-
$parentCategoryIds = array_slice($pathCategoryIds, 2, count($pathCategoryIds) - 3);
61-
62-
if (count($parentCategoryIds)) {
63-
$collection = $this->collectionFactory->create();
64-
$collection->addAttributeToSelect(['name', 'url_key']);
65-
$collection->addAttributeToFilter('entity_id', $parentCategoryIds);
66-
67-
foreach ($collection as $category) {
68-
$breadcrumbs[] = [
69-
'category_id' => $category->getId(),
70-
'category_name' => $category->getName(),
71-
'category_level' => $category->getLevel(),
72-
'category_url_key' => $category->getUrlKey(),
73-
];
74-
}
75-
}
76-
77-
$result = function () use ($breadcrumbs) {
78-
return count($breadcrumbs) ? $breadcrumbs : null;
56+
$result = function () use ($value) {
57+
$breadcrumbsData = $this->breadcrumbsDataProvider->getData($value['path']);
58+
return count($breadcrumbsData) ? $breadcrumbsData : null;
7959
};
80-
8160
return $this->valueFactory->create($result);
8261
}
8362
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\CatalogGraphQl\Model\Resolver\Category\DataProvider;
9+
10+
use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory;
11+
12+
/**
13+
* Breadcrumbs data provider
14+
*/
15+
class Breadcrumbs
16+
{
17+
/**
18+
* @var CollectionFactory
19+
*/
20+
private $collectionFactory;
21+
22+
/**
23+
* @param CollectionFactory $collectionFactory
24+
*/
25+
public function __construct(
26+
CollectionFactory $collectionFactory
27+
) {
28+
$this->collectionFactory = $collectionFactory;
29+
}
30+
31+
/**
32+
* @param string $categoryPath
33+
* @return array
34+
*/
35+
public function getData(string $categoryPath): array
36+
{
37+
$breadcrumbsData = [];
38+
39+
$pathCategoryIds = explode('/', $categoryPath);
40+
$parentCategoryIds = array_slice($pathCategoryIds, 2, -1);
41+
42+
if (count($parentCategoryIds)) {
43+
$collection = $this->collectionFactory->create();
44+
$collection->addAttributeToSelect(['name', 'url_key']);
45+
$collection->addAttributeToFilter('entity_id', $parentCategoryIds);
46+
47+
foreach ($collection as $category) {
48+
$breadcrumbsData[] = [
49+
'category_id' => $category->getId(),
50+
'category_name' => $category->getName(),
51+
'category_level' => $category->getLevel(),
52+
'category_url_key' => $category->getUrlKey(),
53+
];
54+
}
55+
}
56+
return $breadcrumbsData;
57+
}
58+
}

0 commit comments

Comments
 (0)