_configValueFactory = $configValueFactory; parent::__construct($context, $registry, $config, $cacheTypeList, $resource, $resourceCollection, $data); } /** * After save handler * * @return $this * @throws \Exception */ public function afterSave() { $time = $this->getData('groups/import/fields/time/value'); $frequency = $this->getData('groups/import/fields/frequency/value'); $frequencyWeekly = \Magento\Cron\Model\Config\Source\Frequency::CRON_WEEKLY; $frequencyMonthly = \Magento\Cron\Model\Config\Source\Frequency::CRON_MONTHLY; $cronExprArray = [ (int)$time[1], # Minute (int)$time[0], # Hour $frequency == $frequencyMonthly ? '1' : '*', # Day of the Month '*', # Month of the Year $frequency == $frequencyWeekly ? '1' : '*', # Day of the Week ]; $cronExprString = join(' ', $cronExprArray); try { /** @var $configValue \Magento\Framework\App\Config\ValueInterface */ $configValue = $this->_configValueFactory->create(); $configValue->load(self::CRON_STRING_PATH, 'path'); $configValue->setValue($cronExprString)->setPath(self::CRON_STRING_PATH)->save(); } catch (\Exception $e) { throw new \Exception(__('We can\'t save the Cron expression.')); } return parent::afterSave(); } }