/** * Init Category * * @return \Mageplaza\Blog\Model\Category */ protected function initCategory() { $categoryId = (int) $this->getRequest()->getParam('category_id'); /** @var \Mageplaza\Blog\Model\Category $category */ $category = $this->categoryFactory->create(); if ($categoryId) { $category->load($categoryId); } $this->coreRegistry->register('mageplaza_blog_category', $category); return $category; }
/** * move Category in tree * * @param $parentId * @param $afterCategoryId * @return $this * @throws \Magento\Framework\Exception\LocalizedException * @throws \Exception */ public function move($parentId, $afterCategoryId) { try { $parent = $this->categoryFactory->create()->load($parentId); } catch (\Magento\Framework\Exception\NoSuchEntityException $e) { throw new \Magento\Framework\Exception\LocalizedException(__('Sorry, but we can\'t move the Category because we can\'t find the new parent Category you selected.'), $e); } if (!$this->getId()) { throw new \Magento\Framework\Exception\LocalizedException(__('Sorry, but we can\'t move the Category because we can\'t find the new parent Category you selected.')); } elseif ($parent->getId() == $this->getId()) { throw new \Magento\Framework\Exception\LocalizedException(__('We can\'t perform this Category move operation because the parent Category matches the child Category.')); } $this->setMovedCategoryId($this->getId()); $oldParentId = $this->getParentId(); $oldParentIds = $this->getParentIds(); $eventParams = [$this->_eventObject => $this, 'parent' => $parent, 'category_id' => $this->getId(), 'prev_parent_id' => $oldParentId, 'parent_id' => $parentId]; $this->_getResource()->beginTransaction(); try { $this->_eventManager->dispatch($this->_eventPrefix . '_move_before', $eventParams); $this->getResource()->changeParent($this, $parent, $afterCategoryId); $this->_eventManager->dispatch($this->_eventPrefix . '_move_after', $eventParams); $this->_getResource()->commit(); // Set data for indexer $this->setAffectedCategoryIds([$this->getId(), $oldParentId, $parentId]); } catch (\Exception $e) { $this->_getResource()->rollBack(); throw $e; } $this->_eventManager->dispatch($this->_eventPrefix . '_move', $eventParams); $this->_cacheManager->clean([self::CACHE_TAG]); return $this; }
/** * Creates Category model * * @param array $data * @return \Mageplaza\Blog\Model\Category */ public function createCategory($data = []) { return $this->categoryFactory->create($data); }