Save.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <?php
  2. /**
  3. *
  4. * Copyright © Magento, Inc. All rights reserved.
  5. * See COPYING.txt for license details.
  6. */
  7. namespace Magento\Variable\Controller\Adminhtml\System\Variable;
  8. /**
  9. * Save variable POST controller
  10. *
  11. * @api
  12. * @since 100.0.2
  13. */
  14. class Save extends \Magento\Variable\Controller\Adminhtml\System\Variable
  15. {
  16. /**
  17. * Save Action
  18. *
  19. * @return \Magento\Backend\Model\View\Result\Redirect
  20. */
  21. public function execute()
  22. {
  23. $variable = $this->_initVariable();
  24. $data = $this->getRequest()->getPost('variable');
  25. $back = $this->getRequest()->getParam('back', false);
  26. /** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
  27. $resultRedirect = $this->resultRedirectFactory->create();
  28. if ($data) {
  29. $data['variable_id'] = $variable->getId();
  30. $variable->setData($data);
  31. try {
  32. $variable->save();
  33. $this->messageManager->addSuccess(__('You saved the custom variable.'));
  34. if ($back) {
  35. $resultRedirect->setPath(
  36. 'adminhtml/*/edit',
  37. ['_current' => true, 'variable_id' => $variable->getId()]
  38. );
  39. } else {
  40. $resultRedirect->setPath('adminhtml/*/');
  41. }
  42. return $resultRedirect;
  43. } catch (\Exception $e) {
  44. $this->messageManager->addError($e->getMessage());
  45. return $resultRedirect->setPath('adminhtml/*/edit', ['_current' => true]);
  46. }
  47. }
  48. return $resultRedirect->setPath('adminhtml/*/');
  49. }
  50. }