Answers.Body: <p>You're over-thinking it a little bit. To save your widget settings, you don't need a controller class, you only need </p>
  
  <p>a) a method called <code>defineSettings()</code> in your widget class, returning the settings you need, e.g. </p>
  
  <pre><code>protected function defineSettings()
  {
    return array(
      'url'   =&gt; array(AttributeType::Url, 'required' =&gt; true, 'label' =&gt; 'URL'),
      'title' =&gt; array(AttributeType::Name, 'required' =&gt; true),
      'limit' =&gt; array(AttributeType::Number, 'min' =&gt; 0, 'default' =&gt; 5),
    );
  }
  </code></pre>
  
  <p>You can take a look at the <a href="https://github.com/pixelandtonic/Craft-Release/blob/master/app/enums/AttributeType.php" rel="nofollow"><code>AttributeType</code></a> class to see which attribute types Craft support.</p>
  
  <p>b) a template to render your settings – you don't actually need to create the actual <code>&lt;form&gt;</code> element or add hidden <code>action</code> or <code>redirect</code> 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 <code>defineSettings()</code> example method above (this is the whole template):</p>
  
  <pre><code>{% 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')
  }) }}
  </code></pre>
  
  <p>Save the template to <code>/yourplugin/templates/widgethandle_settings.html</code>. Finally, you need </p>
  
  <p>c) a method called <code>getSettingsHtml()</code>, also in your widget class, which simply renders the settings template you added above:</p>
  
  <pre><code>public function getSettingsHtml()
  {
      return craft()-&gt;templates-&gt;render('yourplugin/widgettitle_settings', array(
          'settings' =&gt; $this-&gt;getSettings()
      ));
  }
  </code></pre>
  
  <p>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.</p>
  
  <p>To be clear, this is actually exactly the same way <em>plugin</em> settings work, although for plugins themselves the <code>defineSettings()</code> and <code>getSettingsHtml()</code> method should exist in the primary plugin class. In short – you don't need a controller and you don't need the <code>&lt;form&gt;</code> tag, neither for plugin nor widget settings.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-09-20T08:45:15.687Z
Answers.Text: 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
Answers.UserDisplayName: Robin Schambach
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-09-20T11:09:09.663Z
Answers.Text: @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](https://craftcms.com/docs/plugins/widgets#giving-your-widget-some-settings) 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.
Answers.UserDisplayName: Mats Mikkel Rummelhoff
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-09-20T11:09:15.030Z
Answers.Text: 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 :)
Answers.UserDisplayName: Mats Mikkel Rummelhoff
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-09-20T11:20:48.400Z
Answers.Text: 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
Answers.UserDisplayName: Robin Schambach
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-09-20T06:34:03.547Z
Answers.LastActivityDate: 2016-09-20T06:34:03.547Z
Answers.UserDisplayName: Mats Mikkel Rummelhoff
Body: <p>How do you save Widget Settings?</p>
  
  <p>I read this tutorial <a href="https://craftcms.com/docs/plugins/widgets" rel="nofollow">https://craftcms.com/docs/plugins/widgets</a> several times but I could not find an answer. I'm new to Craft and this is my first plugin.</p>
  
  <p>If I want to save plugin settings I create a Twig template like this</p>
  
  <pre><code>&lt;form method="post" action="" accept-charset="UTF-8"&gt;
          &lt;input type="hidden" name="action" value="sRGoogleAnalytics/settings/saveSettings"&gt;
          &lt;input type="hidden" name="redirect" value="srgoogleanalytics/settings"&gt;
          &lt;input class="btn submit" type="submit" value="{{ 'Submit'|t }}"&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>and know my function <code>public function actionSaveSettings()</code> in my <code>SRGoogleAnalytics_SettingsController</code> 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.</p>
  
  <p>Could you please explain what I have to do exactly in order to get a callback into a specific function?</p>
  
  <p>Thank you very much</p>
  
Tags: <plugin-development><settings><dashboard-widgets>
Title: How to save widget settings
ThreadText: # How to save widget settings
  
  (Asked by: username_0 on 2016-09-19)
  
  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
  
  **Tags:** plugin-development, settings, dashboard-widgets
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2016-09-20. Score: 1 | Accepted answer
  
  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`](https://github.com/pixelandtonic/Craft-Release/blob/master/app/enums/AttributeType.php) 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.
  
  > **Comments:**
  > **username_0**: 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  
  > **username_1**: @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](https://craftcms.com/docs/plugins/widgets#giving-your-widget-some-settings) 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.  
  > **username_1**: 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 :)  
  > **username_0**: 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  
  
  ---
  
  