Skip to content

Commit b09c410

Browse files
author
Stanislav Idolov
authored
ENGCOM-2421: Added breadcrumbs support #112
2 parents 3a57e65 + 606fb83 commit b09c410

File tree

3 files changed

+128
-0
lines changed

3 files changed

+128
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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;
9+
10+
use \Magento\CatalogGraphQl\Model\Resolver\Category\DataProvider\Breadcrumbs as BreadcrumbsDataProvider;
11+
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
12+
use Magento\Framework\GraphQl\Config\Element\Field;
13+
use Magento\Framework\GraphQl\Query\ResolverInterface;
14+
use Magento\Framework\GraphQl\Query\Resolver\Value;
15+
use Magento\Framework\GraphQl\Query\Resolver\ValueFactory;
16+
17+
/**
18+
* Retrieves breadcrumbs
19+
*/
20+
class Breadcrumbs implements ResolverInterface
21+
{
22+
/**
23+
* @var BreadcrumbsDataProvider
24+
*/
25+
private $breadcrumbsDataProvider;
26+
27+
/**
28+
* @var ValueFactory
29+
*/
30+
private $valueFactory;
31+
32+
/**
33+
* @param BreadcrumbsDataProvider $breadcrumbsDataProvider
34+
* @param ValueFactory $valueFactory
35+
*/
36+
public function __construct(
37+
BreadcrumbsDataProvider $breadcrumbsDataProvider,
38+
ValueFactory $valueFactory
39+
) {
40+
$this->breadcrumbsDataProvider = $breadcrumbsDataProvider;
41+
$this->valueFactory = $valueFactory;
42+
}
43+
44+
/**
45+
* @inheritdoc
46+
*/
47+
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null): Value
48+
{
49+
if (!isset($value['path'])) {
50+
$result = function () {
51+
return null;
52+
};
53+
return $this->valueFactory->create($result);
54+
}
55+
56+
$result = function () use ($value) {
57+
$breadcrumbsData = $this->breadcrumbsDataProvider->getData($value['path']);
58+
return count($breadcrumbsData) ? $breadcrumbsData : null;
59+
};
60+
return $this->valueFactory->create($result);
61+
}
62+
}
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+
}

Diff for: app/code/Magento/CatalogGraphQl/etc/schema.graphqls

+8
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,14 @@ interface CategoryInterface @typeResolver(class: "Magento\\CatalogGraphQl\\Model
381381
currentPage: Int = 1 @doc(description: "Specifies which page of results to return. The default value is 1."),
382382
sort: ProductSortInput @doc(description: "Specifies which attribute to sort on, and whether to return the results in ascending or descending order.")
383383
): CategoryProducts @doc(description: "The list of products assigned to the category") @resolver(class: "Magento\\CatalogGraphQl\\Model\\Resolver\\Category\\Products")
384+
breadcrumbs: [Breadcrumb] @doc(description: "Breadcrumbs, parent categories info") @resolver(class: "Magento\\CatalogGraphQl\\Model\\Resolver\\Category\\Breadcrumbs")
385+
}
386+
387+
type Breadcrumb @doc(description: "Breadcrumb item"){
388+
category_id: Int @doc(description: "Category ID")
389+
category_name: String @doc(description: "Category name")
390+
category_level: Int @doc(description: "Category level")
391+
category_url_key: String @doc(description: "Category URL key")
384392
}
385393

386394
type CustomizableRadioOption implements CustomizableOptionInterface @doc(description: "CustomizableRadioOption contains information about a set of radio buttons that are defined as part of a customizable option") {

0 commit comments

Comments
 (0)