/** * Inserts provided user configuration data into database * * @return void */ private function updateUserConfigData() { foreach ($this->request as $key => $val) { $configModel = $this->configFactory->create(); $configModel->setDataByPath($key, $val); $configModel->save(); } }
/** * Save confoguration values * * @param array $data Array of config data * @return void */ public function execute($data) { // 1. transform data into magento config sections format $sections = []; foreach ($data as $path => $value) { $pathParts = explode('/', $path); $section = array_shift($pathParts); if (!isset($sections[$section])) { $sections[$section] = ['section' => $section, 'groups' => []]; } $limit = count($pathParts) - 1; $temp = []; for ($i = $limit; $i >= 0; $i--) { if ($i === $limit) { $value = ['value' => $value]; } elseif ($i === $limit - 1) { $value = ['fields' => $temp]; } else { // when nesting level > 2 (prolabels) $value = ['groups' => $temp]; } $temp = [$pathParts[$i] => $value]; } $sections[$section]['groups'] = array_merge_recursive($sections[$section]['groups'], $temp); } // 2. save config foreach ($this->getStoreIds() as $storeId) { if (!$storeId) { // all stores selected $website = null; $store = null; } else { if (!$this->storeManager->getStore($storeId)->getId()) { continue; } $website = $this->storeManager->getStore($storeId)->getWebsite()->getCode(); $store = $this->storeManager->getStore($storeId)->getCode(); } foreach ($sections as $section) { $configData = ['section' => $section['section'], 'website' => $website, 'store' => $store, 'groups' => $section['groups']]; try { /** @var \Magento\Config\Model\Config $configModel */ $configModel = $this->configFactory->create(['data' => $configData]); $configModel->save(); } catch (\Exception $e) { $this->fault('configuration_save', $e); } } } }
/** * Save configuration * * @return \Magento\Backend\Model\View\Result\Redirect */ public function execute() { try { // custom save logic $this->_saveSection(); $section = $this->getRequest()->getParam('section'); $website = $this->getRequest()->getParam('website'); $store = $this->getRequest()->getParam('store'); $configData = ['section' => $section, 'website' => $website, 'store' => $store, 'groups' => $this->_getGroupsForSave()]; /** @var \Magento\Config\Model\Config $configModel */ $configModel = $this->_configFactory->create(['data' => $configData]); $configModel->save(); $this->messageManager->addSuccess(__('You saved the configuration.')); } catch (\Magento\Framework\Exception\LocalizedException $e) { $messages = explode("\n", $e->getMessage()); foreach ($messages as $message) { $this->messageManager->addError($message); } } catch (\Exception $e) { $this->messageManager->addException($e, __('An error occurred while saving this configuration:') . ' ' . $e->getMessage()); } $this->_saveState($this->getRequest()->getPost('config_state')); /** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */ $resultRedirect = $this->resultRedirectFactory->create(); return $resultRedirect->setPath('adminhtml/system_config/edit', ['_current' => ['section', 'website', 'store'], '_nosid' => true]); }
/** * Saves currency symbol to config * * @param $symbols array * @return $this */ public function setCurrencySymbolsData($symbols = []) { foreach ($this->getCurrencySymbolsData() as $code => $values) { if (isset($symbols[$code])) { if ($symbols[$code] == $values['parentSymbol'] || empty($symbols[$code])) { unset($symbols[$code]); } } } if ($symbols) { $value['options']['fields']['customsymbol']['value'] = serialize($symbols); } else { $value['options']['fields']['customsymbol']['inherit'] = 1; } $this->_configFactory->create()->setSection(self::CONFIG_SECTION)->setWebsite(null)->setStore(null)->setGroups($value)->save(); $this->_eventManager->dispatch('admin_system_config_changed_section_currency_before_reinit', ['website' => $this->_websiteId, 'store' => $this->_storeId]); // reinit configuration $this->_coreConfig->reinit(); $this->_storeManager->reinitStores(); $this->clearCache(); //Reset symbols cache since new data is added $this->_symbolsData = []; $this->_eventManager->dispatch('admin_system_config_changed_section_currency', ['website' => $this->_websiteId, 'store' => $this->_storeId]); return $this; }
/** * @param string $addresses * @return bool */ public function setAddresses($addresses) { $config = $this->configFactory->create(); $config->setDataByPath('profiler/general/addresses', $addresses); $config->save(); return true; }
/** * Initialize objects required to render config form * * @return $this */ protected function _initObjects() { $this->_configDataObject = $this->_configFactory->create(['data' => ['section' => $this->getSectionCode(), 'website' => $this->getWebsiteCode(), 'store' => $this->getStoreCode()]]); $this->_configData = $this->_configDataObject->load(); $this->_fieldsetRenderer = $this->_fieldsetFactory->create(); $this->_fieldRenderer = $this->_fieldFactory->create(); return $this; }
/** * Save config to core_config_data and module storage table. * * @param $path * @param $scopeId * @param $value * */ protected function _saveConfig($path, $scopeId, $value) { // do not save config if path validation fails. if (!($fullPathParts = $this->_validateFullPath($path))) { return; } // get the path from the parts of path $path = implode('/', array_slice($fullPathParts, 1, 3)); $configData = ['section' => 'dev', 'website' => null, 'store' => null, 'groups' => ['jzahedieh_fileconfigurator' => ['fields' => ['test' => 'testval']]]]; /** @var \Magento\Config\Model\Config $configModel */ $configModel = $this->_configFactory->create(['data' => $configData]); /** * todo: work around session issue, more barebones approach using the resource model needed I think. * Area code not set: Area code must be set before starting a session. **/ //$configModel->save(); //todo save in table }
/** * Save configuration * * @return \Magento\Backend\Model\View\Result\Redirect */ public function execute() { try { $section = $this->getRequest()->getParam('section'); $container = $this->getRequest()->getParam('container'); $store = $this->getRequest()->getParam('store'); $configData = ['section' => $section, 'container' => $container, 'store' => $store, 'groups' => $this->getGroupsForSave()]; /** @var \Magento\Config\Model\Config $configModel */ $configModel = $this->configFactory->create(['data' => $configData]); $configModel->save(); $this->messageManager->addSuccess(__('You saved the configuration.')); } catch (\Magento\Framework\Exception\LocalizedException $e) { $messages = explode("\n", $e->getMessage()); foreach ($messages as $message) { $this->messageManager->addError($message); } } catch (\Exception $e) { $this->messageManager->addException($e, __('Something went wrong while saving this configuration:') . ' ' . $e->getMessage()); } /** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */ $resultRedirect = $this->resultRedirectFactory->create(); return $resultRedirect->setPath('*/*/edit', ['_current' => ['section', 'container', 'store'], '_nosid' => true]); }