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?
2 Answers 2
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
}
}
-
1This is actually what I've been doing and was thinking it will be nice if there is a way to dynamically disable them. Now I know we can't. Thanks a lot! :)xenon– xenon2016年12月07日 14:02:57 +00:00Commented Dec 7, 2016 at 14:02
-
Can I use this in
Vendor/Module/Plugin/Customer/Account/CreatePost.phptoo?Anto S– Anto S2020年12月16日 10:50:14 +00:00Commented Dec 16, 2020 at 10:50 -
1@Butterfly. Sure. You can use it anywhere you want.Marius– Marius2020年12月16日 10:57:29 +00:00Commented Dec 16, 2020 at 10:57
An alternative solution is to apply an patch, using https://github.com/cweagans/composer-patches. This composer module applies a patch from a local or remote file to any dependency installed by Composer.
Let me explain.
Assume you have the following <preference for="\Magento\Module\Base" type="\Vendor\Module\Preference"/> in composer module vendor/module.
Firstly, to remove the preference create a patch file patches/vendor/module/etc/di.patch like:
--- a/etc/di.xml 2024年10月14日 10:00:00.000000000 +0200
+++ b/etc/di.xml 2024年10月14日 10:00:00.000000000 +0200
@@ -1,8 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
- <preference for="\Magento\Module\Base" type="\Vendor\Module\Preference"/>
-
Secondly, update you composer.json file in the extra.patches section with a snippet that will apply the patch:
"vendor/module": {
"remove preference" : "patches/vendor/module/etc/di.patch"
}
Thirdly, when you run composer install the patch will be applied if all went well, and the preferences is removed.
The output will look like:
Gathering patches for root package.
...
Removing package vendor/module so that it can be re-installed and re-patched.
- Removing vendor/module (1.0.0)
...
Gathering patches for root package.
Gathering patches for dependencies. This might take a minute.
- Applying patches for vendor/module
patches/vendor/module/etc/di.patch (remove preference)