Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CategoryTree.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
9 
14 
19 {
23  const CATEGORY_INTERFACE = 'CategoryInterface';
24 
28  private $categoryTree;
29 
33  public function __construct(
34  \Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider\CategoryTree $categoryTree
35  ) {
36  $this->categoryTree = $categoryTree;
37  }
38 
44  private function getCategoryId(array $args) : int
45  {
46  if (!isset($args['id'])) {
47  throw new GraphQlInputException(__('"id for category should be specified'));
48  }
49 
50  return (int)$args['id'];
51  }
52 
56  public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
57  {
58  if (isset($value[$field->getName()])) {
59  return $value[$field->getName()];
60  }
61 
62  $rootCategoryId = $this->getCategoryId($args);
63  $categoriesTree = $this->categoryTree->getTree($info, $rootCategoryId);
64  if (!empty($categoriesTree)) {
65  return current($categoriesTree);
66  } else {
67  return null;
68  }
69  }
70 }
__construct(\Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider\CategoryTree $categoryTree)
__()
Definition: __.php:13
$value
Definition: gender.phtml:16
foreach( $_productCollection as $_product)() ?>" class $info
Definition: listing.phtml:52
resolve(Field $field, $context, ResolveInfo $info, array $value=null, array $args=null)