1

How do you save Widget Settings?

I read this tutorial https://craftcms.com/docs/plugins/widgets several times but I could not find an answer. I'm new to Craft and this is my first plugin.

If I want to save plugin settings I create a Twig template like this

<form method="post" action="" accept-charset="UTF-8">
        <input type="hidden" name="action" value="sRGoogleAnalytics/settings/saveSettings">
        <input type="hidden" name="redirect" value="srgoogleanalytics/settings">
        <input class="btn submit" type="submit" value="{{ 'Submit'|t }}">
</form>

and know my function public function actionSaveSettings() in my SRGoogleAnalytics_SettingsController is going to be executed but I don't know how to do the same for widget settings since the widget itself already has a "submit" button and when I create a form in the template there will be two submits.

Could you please explain what I have to do exactly in order to get a callback into a specific function?

Thank you very much

1

You're over-thinking it a little bit. To save your widget settings, you don't need a controller class, you only need

a) a method called defineSettings() in your widget class, returning the settings you need, e.g.

protected function defineSettings()
{
  return array(
    'url'   => array(AttributeType::Url, 'required' => true, 'label' => 'URL'),
    'title' => array(AttributeType::Name, 'required' => true),
    'limit' => array(AttributeType::Number, 'min' => 0, 'default' => 5),
  );
}

You can take a look at the AttributeType class to see which attribute types Craft support.

b) a template to render your settings – you don't actually need to create the actual <form> element or add hidden action or redirect inputs in this template; Craft wraps the fields you render inside a form tag. Here's an example settings template corresponding to the settings from the defineSettings() example method above (this is the whole template):

{% import "_includes/forms" as forms %}

{{ forms.textField({
  label: "URL"|t,
  id: 'url',
  name: 'url',
  value: settings.url,
  required: true,
  errors: settings.getErrors('url')
}) }}

{{ forms.textField({
  label: "Title"|t,
  id: 'title',
  name: 'title',
  value: settings.title,
  required: true,
  errors: settings.getErrors('title')
}) }}

{{ forms.textField({
  label: "Limit"|t,
  id: 'limit',
  name: 'limit',
  value: settings.limit,
  size: 2,
  errors: settings.getErrors('limit')
}) }}

Save the template to /yourplugin/templates/widgethandle_settings.html. Finally, you need

c) a method called getSettingsHtml(), also in your widget class, which simply renders the settings template you added above:

public function getSettingsHtml()
{
    return craft()->templates->render('yourplugin/widgettitle_settings', array(
        'settings' => $this->getSettings()
    ));
}

With these three things in place, you should be able to click the cogwheel that appears when you mouse over your widget in the Dashboard, which will display your settings form.

To be clear, this is actually exactly the same way plugin settings work, although for plugins themselves the defineSettings() and getSettingsHtml() method should exist in the primary plugin class. In short – you don't need a controller and you don't need the <form> tag, neither for plugin nor widget settings.

| improve this answer | |
  • thank you very much but where is the callback then? what is executed when the user clicks on "save" I need to execute several things after the user does that – Robin Schambach Sep 20 '16 at 8:45
  • @Anubarak There's no callback or event that fires when the settings are saved, but you can add some code to the widget class' prepSettings() method (see the official docs for an example), which will run just before the new settings are saved. Also, be aware that the widget template will re-render after the settings are saved, so you could also add code to the getSettingsHtml() method if need be. – Mats Mikkel Rummelhoff Sep 20 '16 at 11:09
  • If you want to edit your original question with some specifics about the "several things" that should be "exectuted" when the settings are saved, I might be able to update my answer with a better response :) – Mats Mikkel Rummelhoff Sep 20 '16 at 11:09
  • Alright I didn't know the widget is re-rendered after it is saved. To be more specific I'm going to create a google analytics dashboard plugin, so I thought I have to "activate" all the functions to render the charts again, but If the widget does that (instead of just flipping around) everything is clear. Thank you very much – Robin Schambach Sep 20 '16 at 11:20

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.