سؤال

I currently have the following <preference/> in one of my di.xml file:

<preference for="Magento\Contact\Controller\Index\Post" type="RadTest\TestModule\Controller\Contact\Post" />

I have an enable/disable configuration option for my module in the admin panel. I only want the <preference> to be enabled when my custom configuration option is set to enabled.

How can I dynamically enable and disable the <preference/> overriding according to the configuration of my module is set?

هل كانت مفيدة؟

المحلول

you cannot enable and/or disable preferences based on a config setting.
di.xml is just configuration. you cannot have logic in it, but you can do something else.
You can have in your class a condition that checks your config flag and does some action accordingly.
I assume your class RadTest\TestModule\Controller\Contact\Post extends Magento\Contact\Controller\Index\Post because you have to override at least one method.
let's say you have to override the method execute.
You can make your class do this:

namespace RadTest\TestModule\Controller\Contact;
class Post extends \Magento\Contact\Controller\Index\Post
{
    ....
    public function execute()
    {
        if (your config setting is disabled) {
            return parent::execute();
        }
        //your custom logic here
    }
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى magento.stackexchange
scroll top