/** * {@inheritdoc} */ public function save(\Magento\Catalog\Api\Data\CategoryInterface $category) { $existingData = $category->toFlatArray(); if ($category->getId()) { $existingCategory = $this->get($category->getId()); if (isset($existingData['image']) && is_array($existingData['image'])) { $existingData['image_additional_data'] = $existingData['image']; unset($existingData['image']); } $existingData['id'] = $existingCategory->getId(); $existingData['parent_id'] = $existingCategory->getParentId(); $existingData['path'] = $existingCategory->getPath(); $existingData['is_active'] = $existingCategory->getIsActive(); $existingData['include_in_menu'] = isset($existingData['include_in_menu']) ? (bool) $existingData['include_in_menu'] : false; $category->setData($existingData); } else { $parentId = $category->getParentId() ?: $this->storeManager->getStore()->getRootCategoryId(); $parentCategory = $this->get($parentId); $existingData['include_in_menu'] = isset($existingData['include_in_menu']) ? (bool) $existingData['include_in_menu'] : false; /** @var $category Category */ $category->setData($existingData); $category->setPath($parentCategory->getPath()); } try { $this->validateCategory($category); $this->categoryResource->save($category); } catch (\Exception $e) { throw new CouldNotSaveException(__('Could not save category: %1', $e->getMessage()), $e); } unset($this->instances[$category->getId()]); return $category; }
/** * Build category URL path * * @param \Magento\Catalog\Api\Data\CategoryInterface|\Magento\Framework\Model\AbstractModel $category * @return string */ public function getUrlPath($category) { if ($category->getParentId() == Category::TREE_ROOT_ID) { return ''; } $path = $category->getUrlPath(); if ($path !== null && !$category->dataHasChangedFor('url_key') && !$category->dataHasChangedFor('parent_id')) { return $path; } $path = $category->getUrlKey(); if ($path === false) { return $category->getUrlPath(); } if ($this->isNeedToGenerateUrlPathForParent($category)) { $parentPath = $this->getUrlPath($this->categoryRepository->get($category->getParentId())); $path = $parentPath === '' ? $path : $parentPath . '/' . $path; } return $path; }