I am using Magento Open Source Edition 2.4.2. I have a custom module where I have an admin configuration like below which is under 'Stores > Custom Menu'.
In the system.xml, I have added the backend model for the 'dropdown field - Import Custom Data' as 'Vendor\Module\Model\Config\Backend\SetTimestamp'
Here is my 'SetTimestamp.php file':
/**
* @param \Magento\Framework\Model\Context $context
* @param \Magento\Framework\Registry $registry
* @param \Magento\Framework\App\Config\ScopeConfigInterface $config
* @param \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList
* @param WriterInterface $configWriter
* @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
* @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
* @param array $data
*/
public function __construct(
\Magento\Framework\Model\Context $context,
\Magento\Framework\Registry $registry,
\Magento\Framework\App\Config\ScopeConfigInterface $config,
\Magento\Framework\App\Cache\TypeListInterface $cacheTypeList,
WriterInterface $configWriter,
\Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
\Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
array $data = []
) {
$this->configWriter = $configWriter;
parent::__construct($context, $registry, $config, $cacheTypeList, $resource, $resourceCollection, $data);
}
/**
* Change the Timestamp value to 0 if the Import Customer option gets changed
*
* @return $this
*/
public function afterSave()
{
$scope = $this->getData('scope');
$scopeId = $this->getData('scope_id');
if ($this->isValueChanged()) {
$this->configWriter->save(
self::TIMESTAMP_VALUE_FIELD,
0,
$scope,
$scopeId
);
}
return parent::afterSave();
}
But, this code doesn't set the value '0' instead it still has the long timestamp value.
Can anyone please help?