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 
8 use Magento\Framework\App\Action\HttpPostActionInterface as HttpPostActionInterface;
10 
18 {
24  protected $_configFactory;
25 
29  protected $_cache;
30 
34  protected $string;
35 
44  public function __construct(
45  \Magento\Backend\App\Action\Context $context,
46  \Magento\Config\Model\Config\Structure $configStructure,
47  \Magento\Config\Controller\Adminhtml\System\ConfigSectionChecker $sectionChecker,
48  \Magento\Config\Model\Config\Factory $configFactory,
49  \Magento\Framework\Cache\FrontendInterface $cache,
50  \Magento\Framework\Stdlib\StringUtils $string
51  ) {
52  parent::__construct($context, $configStructure, $sectionChecker);
53  $this->_configFactory = $configFactory;
54  $this->_cache = $cache;
55  $this->string = $string;
56  }
57 
63  protected function _getGroupsForSave()
64  {
65  $groups = $this->getRequest()->getPost('groups');
66  $files = $this->getRequest()->getFiles('groups');
67 
68  if ($files && is_array($files)) {
73  foreach ($files as $groupName => $group) {
75  if (!empty($data)) {
76  if (!empty($groups[$groupName])) {
77  $groups[$groupName] = array_merge_recursive((array)$groups[$groupName], $data);
78  } else {
79  $groups[$groupName] = $data;
80  }
81  }
82  }
83  }
84  return $groups;
85  }
86 
93  protected function _processNestedGroups($group)
94  {
95  $data = [];
96 
97  if (isset($group['fields']) && is_array($group['fields'])) {
98  foreach ($group['fields'] as $fieldName => $field) {
99  if (!empty($field['value'])) {
100  $data['fields'][$fieldName] = ['value' => $field['value']];
101  }
102  }
103  }
104 
105  if (isset($group['groups']) && is_array($group['groups'])) {
106  foreach ($group['groups'] as $groupName => $groupData) {
107  $nestedGroup = $this->_processNestedGroups($groupData);
108  if (!empty($nestedGroup)) {
109  $data['groups'][$groupName] = $nestedGroup;
110  }
111  }
112  }
113 
114  return $data;
115  }
116 
122  protected function _saveSection()
123  {
124  $method = '_save' . $this->string->upperCaseWords($this->getRequest()->getParam('section'), '_', '');
125  if (method_exists($this, $method)) {
126  $this->{$method}();
127  }
128  }
129 
135  protected function _saveAdvanced()
136  {
137  $this->_cache->clean();
138  }
139 
145  public function execute()
146  {
147  try {
148  // custom save logic
149  $this->_saveSection();
150  $section = $this->getRequest()->getParam('section');
151  $website = $this->getRequest()->getParam('website');
152  $store = $this->getRequest()->getParam('store');
153 
154  $configData = [
155  'section' => $section,
156  'website' => $website,
157  'store' => $store,
158  'groups' => $this->_getGroupsForSave(),
159  ];
161  $configModel = $this->_configFactory->create(['data' => $configData]);
162  $configModel->save();
163  $this->_eventManager->dispatch('admin_system_config_save', [
164  'configData' => $configData,
165  'request' => $this->getRequest()
166  ]);
167  $this->messageManager->addSuccess(__('You saved the configuration.'));
168  } catch (\Magento\Framework\Exception\LocalizedException $e) {
169  $messages = explode("\n", $e->getMessage());
170  foreach ($messages as $message) {
171  $this->messageManager->addError($message);
172  }
173  } catch (\Exception $e) {
174  $this->messageManager->addException(
175  $e,
176  __('Something went wrong while saving this configuration:') . ' ' . $e->getMessage()
177  );
178  }
179 
180  $this->_saveState($this->getRequest()->getPost('config_state'));
182  $resultRedirect = $this->resultRedirectFactory->create();
183  return $resultRedirect->setPath(
184  'adminhtml/system_config/edit',
185  [
186  '_current' => ['section', 'website', 'store'],
187  '_nosid' => true
188  ]
189  );
190  }
191 }
$group
Definition: sections.phtml:16
__()
Definition: __.php:13
$message
__construct(\Magento\Backend\App\Action\Context $context, \Magento\Config\Model\Config\Structure $configStructure, \Magento\Config\Controller\Adminhtml\System\ConfigSectionChecker $sectionChecker, \Magento\Config\Model\Config\Factory $configFactory, \Magento\Framework\Cache\FrontendInterface $cache, \Magento\Framework\Stdlib\StringUtils $string)
Definition: Save.php:44
$method
Definition: info.phtml:13
$configFactory
Definition: config_data.php:43
foreach($appDirs as $dir) $files