48 private $extensibleDataObjectConverter;
55 protected $useConfigFields = [
'available_sort_by',
'default_sort_by',
'filter_price_range'];
77 $storeId = (int)$this->storeManager->getStore()->getId();
78 $existingData = $this->getExtensibleDataObjectConverter()
79 ->toNestedArray(
$category, [], \
Magento\Catalog\Api\Data\CategoryInterface::class);
80 $existingData = array_diff_key($existingData, array_flip([
'path',
'level',
'parent_id']));
81 $existingData[
'store_id'] =
$storeId;
84 $metadata = $this->getMetadataPool()->getMetadata(
85 CategoryInterface::class
89 $existingData[$metadata->getLinkField()] =
$category->getData(
90 $metadata->getLinkField()
93 if (isset($existingData[
'image']) && is_array($existingData[
'image'])) {
94 if (!empty($existingData[
'image'][
'delete'])) {
95 $existingData[
'image'] =
null;
97 if (isset($existingData[
'image'][0][
'name']) && isset($existingData[
'image'][0][
'tmp_name'])) {
98 $existingData[
'image'] = $existingData[
'image'][0][
'name'];
100 unset($existingData[
'image']);
105 $parentId =
$category->getParentId() ?: $this->storeManager->getStore()->getRootCategoryId();
106 $parentCategory = $this->
get($parentId,
$storeId);
107 $existingData[
'path'] = $parentCategory->getPath();
108 $existingData[
'parent_id'] = $parentId;
113 $this->categoryResource->save(
$category);
114 }
catch (\Exception $e) {
117 'Could not save category: %1',
123 unset($this->instances[
$category->getId()]);
124 return $this->
get($category->getId(),
$storeId);
130 public function get($categoryId,
$storeId =
null)
133 if (!isset($this->instances[$categoryId][$cacheKey])) {
135 $category = $this->categoryFactory->create();
143 $this->instances[$categoryId][$cacheKey] =
$category;
145 return $this->instances[$categoryId][$cacheKey];
155 $this->categoryResource->delete(
$category);
156 }
catch (\Exception $e) {
159 'Cannot delete category with id %1',
165 unset($this->instances[$categoryId]);
188 foreach ($this->useConfigFields as $field) {
195 if ($validate !==
true) {
196 foreach ($validate as
$code => $error) {
197 if ($error ===
true) {
198 $attribute = $this->categoryResource->getAttribute(
$code)->getFrontend()->getLabel();
199 throw new \Magento\Framework\Exception\LocalizedException(
200 __(
'The "%1" attribute is required. Enter and try again.',
$attribute)
203 throw new \Magento\Framework\Exception\LocalizedException(
__($error));
207 $category->unsetData(
'use_post_data_config');
215 private function getExtensibleDataObjectConverter()
217 if ($this->extensibleDataObjectConverter ===
null) {
219 ->get(\
Magento\Framework\Api\ExtensibleDataObjectConverter::class);
221 return $this->extensibleDataObjectConverter;
227 private function getMetadataPool()
229 if (
null === $this->metadataPool) {
231 ->get(\
Magento\Framework\EntityManager\MetadataPool::class);
deleteByIdentifier($categoryId)
validateCategory(Category $category)
__construct(\Magento\Catalog\Model\CategoryFactory $categoryFactory, \Magento\Catalog\Model\ResourceModel\Category $categoryResource, \Magento\Store\Model\StoreManagerInterface $storeManager)
static singleField($fieldName, $fieldValue)
save(\Magento\Catalog\Api\Data\CategoryInterface $category)