0

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'. enter image description here

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?

2
  • To see what's going on try to xdebug or var_dump the values of $scope, $scopeId and $this->isValueChanged(). May 26 at 18:31
  • isValueChanged() returns the true or false if the dropdown gets changed, $scope value is 'websites', $scopeId value is 1
    – Octopus
    May 27 at 3:29
0

Give a try with following.

Field xml code would be part of adminhtml/system.xml

<field id="timestamp" translate="label" type="text" sortOrder="1" showInDefault="1" showInWebsite="0" showInStore="0">
        <label>SetTimestamp</label>
        <comment>SetTimestamp</comment>
       <backend_model>Vendor\Model\Config\Backend\SetTimestamp</backend_model>
</field>

File: Vendor\MostSold\Model\Config\Backend\SetTimestamp.php

<?php

namespace Vendor\Module\Model\Config\Backend;

/**
 * Class SetTimestamp
 * @package Vendor\Module\Model\Config\Backend
 */
class SetTimestamp extends \Magento\Framework\App\Config\Value
{
    /**
     * @return SetTimestamp
     */
    public function beforeSave()
    {
        $value = $this->getValue();

        if ($this->isValueChanged() && $value !== '0') {
            $this->setValue(0);
        }
        return parent::beforeSave();
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.