ReyDev Posted November 21, 2020 Posted November 21, 2020 When a widget is removed ,I am going to do some clean up on settings values, I've overridden the delete method , but It doesn't work. class _MyWidget extends \IPS\Widget . . public function delete(){ \IPS\Settings::i()->changeValues(['key'=>'value']); }
Adriano Faria Posted November 21, 2020 Posted November 21, 2020 Add parent::delete(); before change the setting value.
ReyDev Posted November 21, 2020 Author Posted November 21, 2020 (edited) 2 hours ago, Adriano Faria said: Add parent::delete(); before change the setting value. Thanks, But doesn't work at all. Do I have to call this method somewhere after deleting the widget? Edited November 21, 2020 by ReyDev
Adriano Faria Posted November 21, 2020 Posted November 21, 2020 Actually, you don’t need to extend anything. Add a uninstall.php file in the root of the plugin folder (same as settings.php) and change the setting value there.
Adriano Faria Posted November 21, 2020 Posted November 21, 2020 (edited) Wait, I’m confused now. By “widget” you mean a plugin that adds a widget, right? Or do you really mean add/remove a widget from the widget containers (sidebar, top or bottom)? Edited November 21, 2020 by Adriano Faria
ReyDev Posted November 21, 2020 Author Posted November 21, 2020 (edited) 1 hour ago, Adriano Faria said: Or do you really mean add/remove a widget from the widget containers (sidebar, top or bottom)? This is exactly what I meant. I am going to clean up something after removing the widget from the sidebar or main area Edited November 21, 2020 by ReyDev
Adriano Faria Posted November 21, 2020 Posted November 21, 2020 You have to extend \IPS\core\modules\front\system\widgets::saveOrder(). This is called when you add/remove a widget from the widgets container.
ReyDev Posted November 21, 2020 Author Posted November 21, 2020 6 hours ago, Adriano Faria said: You have to extend \IPS\core\modules\front\system\widgets::saveOrder(). Thanks, but according to what I've read about widgets, the delete method is called before deleting the widget. This method is used in the WYSIWYG Editor widget, for example So what does this method really do? /** * Before the widget is removed, we can do some clean up * * @return void */ public function delete() { /* Does nothing by default but can be overridden */ }
Adriano Faria Posted November 21, 2020 Posted November 21, 2020 (edited) According to the JS widget file, the function above is called when you add/remove a block. You can debug by using \IPS\Log::Debug and see what shows up in your Console or using \IPS\Log::Log and see the error logs in your ACP. Edited November 21, 2020 by Adriano Faria ReyDev 1
ReyDev Posted November 21, 2020 Author Posted November 21, 2020 11 minutes ago, Adriano Faria said: Have you tested, mate? I did. Anyway, wait for someone else to try to help you. Thank you very much. I'm working on it. I just wanted to know what this method does
Adriano Faria Posted November 21, 2020 Posted November 21, 2020 38 minutes ago, Adriano Faria said: According to the JS widget file, the function above is called when you add/remove a block. You can debug by using \IPS\Log::Debug and see what shows up in your Console or using \IPS\Log::Log and see the error logs in your ACP.
ReyDev Posted November 21, 2020 Author Posted November 21, 2020 (edited) 1 hour ago, ReyDev said: \IPS\core\modules\front\system\widgets::saveOrder(). Do I have to do this by HOOK?! (I'm new to this platform 🙂 ) Edited November 21, 2020 by ReyDev
Miss_B Posted November 22, 2020 Posted November 22, 2020 5 hours ago, ReyDev said: Do I have to do this by HOOK?! (I'm new to this platform 🙂 ) Yes, you will need to do that by creating a hook. ReyDev 1
Martin A. Posted November 22, 2020 Posted November 22, 2020 I'm not sure this is the approach you want to take. What would happen if your widget is used on 10 different pages? If you remove it from one of them this would change the setting for all other pages. You haven't said what this "clean up" is all about. You should look into using the configuration() method in the widget. And/or deal with the settings in the ACP. Look at the relatedContent or stream widget in the core app to see how that's done. If you look at the topContributors widget, the delete method is used to delete a custom language string that's part of the widget configuration.
Daniel F Posted November 22, 2020 Posted November 22, 2020 Why would you want to change a global setting when the widget is removed? What are you trying to achieve?
Solution ReyDev Posted November 22, 2020 Author Solution Posted November 22, 2020 1 hour ago, Martin A. said: I'm not sure this is the approach you want to take. What would happen if your widget is used on 10 different pages? If you remove it from one of them this would change the setting for all other pages. You haven't said what this "clean up" is all about. You should look into using the configuration() method in the widget. And/or deal with the settings in the ACP. Look at the relatedContent or stream widget in the core app to see how that's done. If you look at the topContributors widget, the delete method is used to delete a custom language string that's part of the widget configuration. 58 minutes ago, Daniel F said: Why would you want to change a global setting when the widget is removed? What are you trying to achieve? I solved my problem in another way. But I think this can be useful. When I add the widget, I add a value to the array that is stored in the settings. I do this when configuring the widget. (configuration() method) Now I want to remove this value from the array I defined, when deleting the widget. (I thought the delete method would do that)
Recommended Posts