cookieBuilder = $cookieBuilder; $this->dataPersistor = $dataPersistor; $this->cookieRepository = $cookieRepository; $this->durationDecorator = $durationDecorator; } /** * @return \Magento\Backend\Model\View\Result\Redirect|\Magento\Framework\App\ResponseInterface * @throws \Magento\Framework\Exception\NoSuchEntityException */ public function execute() { $storeId = $this->initCurrentStore(); /** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */ $resultRedirect = $this->resultRedirectFactory->create(); $postData = $this->getRequest()->getPostValue(); $id = (int) $this->getRequest()->getParam('id'); if ($postData) { $cookie = $this->cookieBuilder->build($this->getRequest()); $this->dataPersistor->set(CookieInterface::DATA_PERSISTOR_KEY, $postData); if ($id && ! $cookie->getId()) { $this->messageManager->addErrorMessage(__('This cookie no longer exists.')); return $resultRedirect->setPath('*/*/'); } try { $postData = $this->durationDecorator->serializeParams($postData); $cookie->addData($postData); $this->prepareUseDefault($postData, $cookie); $this->cookieRepository->save($cookie); $this->messageManager->addSuccessMessage(__('You saved the cookie.')); $this->dataPersistor->clear(CookieInterface::DATA_PERSISTOR_KEY); if ($this->getRequest()->getParam('back')) { return $resultRedirect->setPath( '*/*/edit', ['id' => $cookie->getId(), '_current' => true] ); } return $resultRedirect->setPath('*/*/'); } catch (LocalizedException $e) { $this->messageManager->addErrorMessage($e->getMessage()); } catch (\Exception $e) { $this->messageManager->addExceptionMessage( $e, __('Something went wrong while saving the cookie.') ); } $this->dataPersistor->set(CookieInterface::DATA_PERSISTOR_KEY, $postData); return $resultRedirect->setPath('*/*/edit', [ 'id' => $this->getRequest()->getParam('id'), 'store' => $storeId, ]); } return $resultRedirect->setPath('*/*/'); } }