mathRandom = $mathRandom; $this->serializer = $serializer ?: \Magento\Framework\App\ObjectManager::getInstance() ->get(Json::class); parent::__construct($context, $registry, $config, $cacheTypeList, $resource, $resourceCollection, $data); } /** * Prepare data before save * * @return $this */ public function beforeSave() { $value = $this->getValue(); if (!is_array($value)) { try { $value = $this->serializer->unserialize($value); } catch (\InvalidArgumentException $e) { $value = []; } } $result = []; foreach ($value as $data) { if (empty($data['country_id']) || empty($data['cc_types'])) { continue; } $country = $data['country_id']; if (array_key_exists($country, $result)) { $result[$country] = $this->appendUniqueCountries($result[$country], $data['cc_types']); } else { $result[$country] = $data['cc_types']; } } $this->setValue($this->serializer->serialize($result)); return $this; } /** * Process data after load * * @return $this */ public function afterLoad() { if ($this->getValue()) { $value = $this->serializer->unserialize($this->getValue()); if (is_array($value)) { $this->setValue($this->encodeArrayFieldValue($value)); } } return $this; } /** * Encode value to be used in \Magento\Config\Block\System\Config\Form\Field\FieldArray\AbstractFieldArray * * @param array $value * @return array */ protected function encodeArrayFieldValue(array $value) { $result = []; foreach ($value as $country => $creditCardType) { $id = $this->mathRandom->getUniqueHash('_'); $result[$id] = ['country_id' => $country, 'cc_types' => $creditCardType]; } return $result; } /** * Append unique countries to list of exists and reindex keys * * @param array $countriesList * @param array $inputCountriesList * @return array */ private function appendUniqueCountries(array $countriesList, array $inputCountriesList) { $result = array_merge($countriesList, $inputCountriesList); return array_values(array_unique($result)); } }