Jump to content

Some action after a widget is removed


Go to solution Solved by ReyDev,

Recommended Posts

Posted

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']);
}
Posted (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 by ReyDev
Posted (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 by Adriano Faria
Posted (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 by ReyDev
Posted
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 */
}
Posted (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 by Adriano Faria
Posted
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

Posted
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.

 

Posted (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 by ReyDev
Posted

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.

  • Solution
Posted
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)

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...