How to Throw Exception on Magento 2 Admin Save Configuration
Sometimes you may require to throw exception on Magento 2 admin save configuration. Here is the complete solution to do that.
Suppose you have developed a custom Magento 2 module that requires a third-party library or component to function properly. The extension may not work in case of the absence of such required components and may further affect the overall functionality of the store. Showing a simple exception in the backend during the module configuration part can prevent such an instance and help the admin solve the issue instantly.
In this blog post, I have provided a complete programmatic solution to restrict admin from saving configuration in Magento 2.
Method to Throw Exception on Magento 2 Admin Save Configuration
You can use the following steps to throw an exception to the Magento 2 admin while saving the configuration.
First of all, create a registration.php file at the app/code/Vendor/Module/ directory and add the following code to register a custom module:
1 2 3 4 5 6 7 8 9 |
<?php use Magento\Framework\Component\ComponentRegistrar; ComponentRegistrar::register( ComponentRegistrar::MODULE, 'Vendor_Module', __DIR__ ); |
Create a new module.xml file at app/code/Vendor/Module/etc/ directory and add the following code:
1 2 3 4 5 6 |
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd"> <module name="Vendor_Module" setup_version="1.0.0"> </module> </config> |
Add the following code in the di.xml file at app/code/Vendor/Module/etc/ directory:
1 2 3 |
<type name="Magento\Config\Model\Config"> <plugin name="admin_system_config_save_plugin" type="Vendor\Module\Plugin\ConfigPlugin"/> </type> |
Open the ConfigPlugin.php file at app/code/Vendor/Module/Plugin/ directory and add the following code to throw a custom exception in the Magento 2 admin panel:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<?php namespace Vendor\Vendor\Plugin; use Magento\Framework\Exception\InputException; use Magento\Config\Model\Config; use Magento\Framework\Message\ManagerInterface; class ConfigPlugin { public function aroundSave( Config $config, \Closure $proceed ) { if (condition) { throw new InputException(__('Error Message')); } return $proceed(); } } |
In the above code, you can set a custom condition to show the exception and customize the exception text as per your requirements.
That’s it! This is how you can throw exception on Magento 2 admin save configuration. 💻
In case you still have any queries or doubts, feel free to mention them in the comment section below. I would be happy to help. 😊
Also, do not forget to share this post with your Magento 2 friends via social media.
Thanks for reading! 😃
Jignesh Parmar
An expert in his field, Jignesh is the team leader at Meetanshi and a certified Magento developer. His passion for Magento has inspired others in the team too. Apart from work, he is a cricket lover.
Prev
How to Auto Apply Coupon Code in Magento 2
How to Configure Magento 2 Custom Order Number Extension
Next