Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Save.php
Go to the documentation of this file.
1 <?php
7 
15 
19 class Save extends Action
20 {
25 
29  protected $configFactory;
30 
34  protected $dataPersistor;
35 
42  public function __construct(
43  Context $context,
47  ) {
48  $this->designConfigRepository = $designConfigRepository;
49  $this->configFactory = $configFactory;
50  $this->dataPersistor = $dataPersistor;
51  parent::__construct($context);
52  }
53 
59  protected function _isAllowed()
60  {
61  return $this->_authorization->isAllowed('Magento_Config::config_design');
62  }
63 
69  public function execute()
70  {
71  if (!$this->getRequest()->isPost()) {
72  throw new NotFoundException(__('Page not found.'));
73  }
74 
75  $resultRedirect = $this->resultRedirectFactory->create();
76  $scope = $this->getRequest()->getParam('scope');
77  $scopeId = (int)$this->getRequest()->getParam('scope_id');
78  $data = $this->getRequestData();
79 
80  try {
81  $designConfigData = $this->configFactory->create($scope, $scopeId, $data);
82  $this->designConfigRepository->save($designConfigData);
83  $this->messageManager->addSuccessMessage(__('You saved the configuration.'));
84 
85  $this->dataPersistor->clear('theme_design_config');
86 
87  $returnToEdit = (bool)$this->getRequest()->getParam('back', false);
88  $resultRedirect->setPath('theme/design_config/');
89  if ($returnToEdit) {
90  $resultRedirect->setPath('theme/design_config/edit', ['scope' => $scope, 'scope_id' => $scopeId]);
91  }
92  return $resultRedirect;
93  } catch (LocalizedException $e) {
94  $messages = explode("\n", $e->getMessage());
95  foreach ($messages as $message) {
96  $this->messageManager->addErrorMessage(__('%1', $message));
97  }
98  } catch (\Exception $e) {
99  $this->messageManager->addExceptionMessage(
100  $e,
101  __('Something went wrong while saving this configuration:') . ' ' . $e->getMessage()
102  );
103  }
104 
105  $this->dataPersistor->set('theme_design_config', $data);
106 
107  $resultRedirect->setPath('theme/design_config/edit', ['scope' => $scope, 'scope_id' => $scopeId]);
108  return $resultRedirect;
109  }
110 
116  protected function getRequestData()
117  {
118  $data = array_merge(
119  $this->getRequest()->getParams(),
120  $this->getRequest()->getFiles()->toArray()
121  );
122  $data = array_filter($data, function ($param) {
123  return isset($param['error']) && $param['error'] > 0 ? false : true;
124  });
125 
131  if (isset($data['theme_theme_id']) && $data['theme_theme_id'] === '') {
132  $data['theme_theme_id'] = null;
133  }
134  return $data;
135  }
136 }
return false
Definition: gallery.phtml:36
__()
Definition: __.php:13
$message
__construct(Context $context, DesignConfigRepository $designConfigRepository, ConfigFactory $configFactory, DataPersistorInterface $dataPersistor)
Definition: Save.php:42