Skip to content

Commit 34ed6d1

Browse files
committed
19 Added breadcrumbs support
1 parent f947cad commit 34ed6d1

File tree

2 files changed

+91
-0
lines changed

2 files changed

+91
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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\Framework\GraphQl\Schema\Type\ResolveInfo;
11+
use Magento\Framework\GraphQl\Config\Element\Field;
12+
use Magento\Framework\GraphQl\Query\ResolverInterface;
13+
use Magento\Framework\GraphQl\Query\Resolver\Value;
14+
use Magento\Framework\GraphQl\Query\Resolver\ValueFactory;
15+
use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory;
16+
17+
/**
18+
* Retrieves breadcrumbs
19+
*/
20+
class Breadcrumbs implements ResolverInterface
21+
{
22+
/**
23+
* @var CollectionFactory
24+
*/
25+
private $collectionFactory;
26+
27+
/**
28+
* @var ValueFactory
29+
*/
30+
private $valueFactory;
31+
32+
/**
33+
* @param ValueFactory $valueFactory
34+
* @param CollectionFactory $collectionFactory
35+
*/
36+
public function __construct(
37+
ValueFactory $valueFactory,
38+
CollectionFactory $collectionFactory
39+
) {
40+
$this->collectionFactory = $collectionFactory;
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+
$breadcrumbs = [];
50+
51+
if (!isset($value['path'])) {
52+
$result = function () {
53+
return null;
54+
};
55+
return $this->valueFactory->create($result);
56+
}
57+
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;
79+
};
80+
81+
return $this->valueFactory->create($result);
82+
}
83+
}

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)