5

Is there a way to programatically update a plugin's settings? I'm experimenting with setSettings() but not really getting anywhere - I'm still quite new to plugin development so there's a good chance i'm completely off target hehe.

For some context: I'm trying to update the plugin settings when an event is fired and picked up by a listener as follows:

public function init()
{

    // add event listeners
    craft()->on('assets.onReplaceFile', function(Event $event) {

        //Load Asset
        $asset = $event->params['asset'];

        //Check if asset is the svg sprite
        if($asset->getUrl() == $this->getSettings()->spritePath){
            $url = craft()->getSiteUrl().$this->getSettings()->spritePath;
            $this->setSettings(array('spriteDefs'=>$this->getSpriteDefs($url)));
        }

    });
}

getSpriteDefs returns an array.

Thanks in advance!

  • Have you tried a couple of die() statements to see if it's making it into your if statements? – Brad Bell Mar 15 '15 at 4:17
  • Yes it was updating the settings model but not saving it to the db as mentioned in the selected answer below. My question might not have been 100% clear, apologies for that. thanks – Daniel Paras Mar 16 '15 at 7:01
10

It's not 100% clear from your question, but I'm guessing that by "update the settings" you mean actually storing the new settings in the database, which the setSettings() method will not do; it will only update your plugin's settings model. This means that any following calls to e.g. getSettingsHtml() should return the updated data, but it will not persist.

To store the new settings in the database, you'll need to use Craft's PluginService:

craft()->plugins->savePluginSettings( $this, array( 'spriteDefs' => $this->getSpriteDefs( $url ) ) );

The $this variable in the call above refers to an instance of BasePlugin – meaning that the above code will only work if called from your plugins main class. I notice that your code is contained within your plugin's init() method, so you should be fine, however if you ever want to update a plugin's settings from outside the main class (e.g. through a service), you would first need to fetch an instance of the plugin:

$myPlugin = craft()->plugins->getPlugin( 'myPluginHandle' );
craft()->plugins->savePluginSettings( $myPlugin, $settings );
  • Yes thats correct, by update I meant persist - that is the correct term, sorry for the ambiguity - and I'm calling it within the plugin's main class yes. The savePluginsSettings did the trick :) – Daniel Paras Mar 16 '15 at 6:57
  • No worries – temporarily manipulating the settings model within the app session lifetime cycle would be a valid use case as well, where setSettings would be the answer. Happy to hear my answer helped you out! – Mats Mikkel Rummelhoff Mar 16 '15 at 11:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.