public function save(PostInterface $post) { $this->validate($post); $postData = $post->getData(); $postModel = $this->_postFactory->create(); $postModel->load($post->getId()); $postModel->setData($postData); $postModel->save(); $this->_postRegistry->push($postModel); $savedPost = $this->getByUrl($post->getUrlKey()); $this->_eventManager->dispatch('post_save_after_data_object', ['post_data_object' => $savedPost, 'orig_post_data_object' => $post]); return $savedPost; }
/** * Validate and Match Blog Post and modify request * * @param RequestInterface $request * @return bool */ public function match(RequestInterface $request) { $urlKey = str_replace("/blog/", "", $request->getPathInfo()); $urlKey = rtrim($urlKey, '/'); $urlKey = ltrim($urlKey, '/'); /** @var \ISM\Blog\Model\Post $post */ $post = $this->_postFactory->create(); $id = $post->checkUrlKey($urlKey); if (!$id) { return null; } $request->setModuleName('blog')->setControllerName('view')->setActionName('index')->setParam('id', $id); $request->setAlias(Url::REWRITE_REQUEST_PATH_ALIAS, $urlKey); return $this->_actionFactory->create('Magento\\Framework\\App\\Action\\Forward'); }
public function retrieveByUrl($url) { if (isset($this->postRegistryByUrl[$url])) { return $this->postRegistryByUrl[$url]; } /** @var $post Post $post */ $post = $this->postFactory->create(); $id = $post->checkUrlKey($url); if (!$id) { throw NoSuchEntityException::singleField('url_key', $url); } else { $post->load($id); $this->postRegistryById[$id] = $post; $this->postRegistryByUrl[$url] = $post; return $post; } }
/** * Execute post delete. * * @return $this */ public function execute() { $id = $this->getRequest()->getParam('id'); $post = $this->_postFactory->create(); $resultRedirect = $this->resultRedirectFactory->create(); $post->load($id); $img = $post->getImageUrl(); $image = $this->_imageFactory->create(); if (!$post->getId()) { return $resultRedirect->setPath('*/*/'); } try { $post->delete(); } catch (Exception $e) { $this->messageManager->addError($e->getMessage()); } if ($img) { $image->remove(Upload::UPLOAD_POST_IMAGE_DIR . $img); } return $resultRedirect->setPath('*/*/'); }