Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Data Fields
CategoryTree Class Reference

Public Member Functions

 __construct (CollectionFactory $collectionFactory, AttributesJoiner $attributesJoiner, DepthCalculator $depthCalculator, LevelCalculator $levelCalculator, MetadataPool $metadata, Hydrator $hydrator)
 
 getTree (ResolveInfo $resolveInfo, int $rootCategoryId)
 

Data Fields

const DEPTH_OFFSET = 1
 

Detailed Description

Category tree data provider

Definition at line 24 of file CategoryTree.php.

Constructor & Destructor Documentation

◆ __construct()

__construct ( CollectionFactory  $collectionFactory,
AttributesJoiner  $attributesJoiner,
DepthCalculator  $depthCalculator,
LevelCalculator  $levelCalculator,
MetadataPool  $metadata,
Hydrator  $hydrator 
)
Parameters
CollectionFactory$collectionFactory
AttributesJoiner$attributesJoiner
DepthCalculator$depthCalculator
LevelCalculator$levelCalculator
MetadataPool$metadata
Hydrator$hydrator

Definition at line 69 of file CategoryTree.php.

76  {
77  $this->collectionFactory = $collectionFactory;
78  $this->attributesJoiner = $attributesJoiner;
79  $this->depthCalculator = $depthCalculator;
80  $this->levelCalculator = $levelCalculator;
81  $this->metadata = $metadata;
82  $this->hydrator = $hydrator;
83  }

Member Function Documentation

◆ getTree()

getTree ( ResolveInfo  $resolveInfo,
int  $rootCategoryId 
)
Parameters
ResolveInfo$resolveInfo
int$rootCategoryId
Returns
array

Definition at line 90 of file CategoryTree.php.

90  : array
91  {
92  $categoryQuery = $resolveInfo->fieldNodes[0];
93  $collection = $this->collectionFactory->create();
94  $this->joinAttributesRecursively($collection, $categoryQuery);
95  $depth = $this->depthCalculator->calculate($categoryQuery);
96  $level = $this->levelCalculator->calculate($rootCategoryId);
97  //Search for desired part of category tree
98  $collection->addPathFilter(sprintf('.*/%s/[/0-9]*$', $rootCategoryId));
99  $collection->addFieldToFilter('level', ['gt' => $level]);
100  $collection->addFieldToFilter('level', ['lteq' => $level + $depth - self::DEPTH_OFFSET]);
101  $collection->setOrder('level');
102  $collection->getSelect()->orWhere(
103  $this->metadata->getMetadata(CategoryInterface::class)->getIdentifierField() . ' = ?',
105  );
106  return $this->processTree($collection->getIterator());
107  }

Field Documentation

◆ DEPTH_OFFSET

const DEPTH_OFFSET = 1

In depth we need to calculate only children nodes, so the first wrapped node should be ignored

Definition at line 29 of file CategoryTree.php.


The documentation for this class was generated from the following file: