<p>It seems all you miss is the define setting and the value attribute in your template, Craft manages the settings for your plugins without any need to edit many things. It stores and loads them automatically. You can handle the entire process on your own and overwrite craft functions but out of the box you have to do nearly nothing. </p>

<p>As a great boilerplate you could create a plugin with <a href="https://pluginfactory.io/" rel="nofollow noreferrer">Pluginfaktory</a> it has many examples how to manage things like routing, templating and all that kind of stuff. </p>

<p>Just turn the "Settings" lightswitch to on and don't change the "Comments" button.</p>

<p>Anyway here is some example code to store settings
In your Plugin.php you need</p>

<pre><code> /**
 * Defines the attributes that model your plugin’s available settings.
 *
 * @return array
 */
protected function defineSettings()
{
    // this is an array with all the settings the user may change. 
    // Open the `AttributeType` file to see a full list. Eg. Integer, 
    // String, JSON, Enum.... there are many ready to use data types, and of 
    // course you could define validation rules like min length, max length, 
    // in range and such
    return array(
        'apiKey' =&gt; array(AttributeType::String, 'label' =&gt; 'Your Api Key', 'default' =&gt; 'XXXX'),
        'listId' =&gt; array(AttributeType::String),
    );
}

/**
 * Returns the HTML that displays your plugin’s settings.
 *
 * @return mixed
 */
public function getSettingsHtml()
{
   return craft()-&gt;templates-&gt;render('mailchimped/settings', array(
       'settings' =&gt; $this-&gt;getSettings()
   ));
}
</code></pre>

<p>And your template</p>

<pre><code>{% import "_includes/forms" as forms %}

{{ forms.textField({
    label: 'Some Setting',
    instructions: 'Enter some setting here.',
    id: 'apiKey',
    name: 'apiKey', 
    value: settings['apiKey']})
}}

{{ forms.textField({
    label: 'Some Setting',
    instructions: 'Enter some setting here.',
    id: 'listId',
    name: 'listId', 
    value: settings['listId']})
}}
</code></pre>

<p>Yeah, thats finally it... you can access those with from everywhere</p>

<pre><code>MailchimpedPlugin::getSettings()['listId']
</code></pre>

<p>Or</p>

<pre><code>$settings = MailchimpedPlugin::getSettings();
$data = $settings-&gt;getAttributes();
</code></pre>

CC BY-SA 3.0
2017-12-15T02:29:48.040Z
Ill check this out tomorrow, and mark it as the solution if it works for me. Thanks for taking the time.
Dan Zuzevich
CC BY-SA 3.0
2017-12-15T16:10:36.880Z
Thanks a lot man. This is a really great answer. This has been super helpful, it works. Ill prob be posting a few more questions lol. Cheers.
Dan Zuzevich
CC BY-SA 3.0
2017-12-15T16:41:59.160Z
Feel free to ask whatever you like. Craft has a steep learning curve but once you get it you'll be able to create plugins and pages in no time. We tried many systems for about 1.5 years and none of them gave the opportunity to be as fast as with craft + yii.
Robin Schambach
CC BY-SA 3.0
2017-12-15T16:59:09.367Z
I agree. Its great.
Dan Zuzevich
CC BY-SA 3.0
2017-12-14T22:59:15.140Z
2017-12-14T23:12:58.550Z
2017-12-14T23:12:58.550Z
Robin Schambach
<p>I need to create a quick, and small plugin for a client, and am having trouble with the official Craft plugin tutorial.</p>

<p>Here is my base plugin code:</p>

<pre><code>&lt;?php
namespace Craft;

class MailchimpedPlugin extends BasePlugin
{
    function getName()
    {
         return Craft::t('Mailchimped');
    }

    function getVersion()
    {
        return '1.0';
    }

    protected function defineSettings() {
        return array(
            // Very confused about what goes here.
        );
    }

    public function getSettingsHtml() {
        return craft()-&gt;templates-&gt;render('mailchimped/settings', array(
            'settings' =&gt; $this-&gt;getSettings()
        ));
    }

}
</code></pre>

<p>And here is my settings page code w/ screenshot of what it looks like on the front end:</p>

<pre><code>{% import '_includes/forms' as forms %}

&lt;div&gt;
  {{ forms.textField({
    label: "API Key"|t,
    id: 'apiKey',
    name: 'apiKey',
    placeholder: "XXXXXXXXXXXX",
    required: true
  }) }}

  {{ forms.textField({
    label: "List ID"|t,
    id: 'listId',
    name: 'listId',
    placeholder: "d0ffd45cbd",
    required: true
  }) }}

  &lt;h3&gt;Help&lt;/h3&gt;
  &lt;a href="https://kb.mailchimp.com/integrations/api-integrations/about-api-keys" target="_blank"&gt;
    How To Find Your API Key
  &lt;/a&gt;
  &lt;br /&gt;
  &lt;a href="https://kb.mailchimp.com/lists/manage-contacts/find-your-list-id" target="_blank"&gt;
    How To Find Your List ID
  &lt;/a&gt;

&lt;/div&gt;
</code></pre>

<p><a href="https://i.sstatic.net/EECud.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/EECud.png" alt="Craft plugin"></a></p>

<p>Basically, all I want to know how to do, is to get the api key and list id field to save and be persisted after the user presses the save button.</p>

<p>I tried reading through the plugin tutorial, and looked at the sample plugin Github page, but honestly, it has to much functionality for a beginner, and is just confusing me even more.</p>

<p>Ideally, the accepted answer will have some code outlining how I would go about this. P.S. I am not looking for an existing plugin, as I want to to do this myself. My company(A studio) uses CraftCMS for their clients websites, so I want to know how to create custom plugins in the future.</p>

<plugin-development>
Saving Field Data Custom Craft Plugin
# Saving Field Data Custom Craft Plugin

(Asked by: username_0 on 2017-12-14)

I need to create a quick, and small plugin for a client, and am having trouble with the official Craft plugin tutorial.
Here is my base plugin code:
templates->render('mailchimped/settings', array(
            'settings' => $this->getSettings()
        ));
    }

}

And here is my settings page code w/ screenshot of what it looks like on the front end:
{% import '_includes/forms' as forms %}


  {{ forms.textField({
    label: "API Key"|t,
    id: 'apiKey',
    name: 'apiKey',
    placeholder: "XXXXXXXXXXXX",
    required: true
  }) }}

  {{ forms.textField({
    label: "List ID"|t,
    id: 'listId',
    name: 'listId',
    placeholder: "d0ffd45cbd",
    required: true
  }) }}

  Help
  
    How To Find Your API Key
  
  
  
    How To Find Your List ID
  




Basically, all I want to know how to do, is to get the api key and list id field to save and be persisted after the user presses the save button.
I tried reading through the plugin tutorial, and looked at the sample plugin Github page, but honestly, it has to much functionality for a beginner, and is just confusing me even more.
Ideally, the accepted answer will have some code outlining how I would go about this. P.S. I am not looking for an existing plugin, as I want to to do this myself. My company(A studio) uses CraftCMS for their clients websites, so I want to know how to create custom plugins in the future.

**Tags:** plugin-development


---

## 1 answers

### Answer by username_1 on 2017-12-14. Score: 2 | Accepted answer

It seems all you miss is the define setting and the value attribute in your template, Craft manages the settings for your plugins without any need to edit many things. It stores and loads them automatically. You can handle the entire process on your own and overwrite craft functions but out of the box you have to do nearly nothing. 
As a great boilerplate you could create a plugin with Pluginfaktory it has many examples how to manage things like routing, templating and all that kind of stuff. 
Just turn the "Settings" lightswitch to on and don't change the "Comments" button.
Anyway here is some example code to store settings
In your Plugin.php you need
 /**
 * Defines the attributes that model your plugin’s available settings.
 *
 * @return array
 */
protected function defineSettings()
{
    // this is an array with all the settings the user may change. 
    // Open the `AttributeType` file to see a full list. Eg. Integer, 
    // String, JSON, Enum.... there are many ready to use data types, and of 
    // course you could define validation rules like min length, max length, 
    // in range and such
    return array(
        'apiKey' => array(AttributeType::String, 'label' => 'Your Api Key', 'default' => 'XXXX'),
        'listId' => array(AttributeType::String),
    );
}

/**
 * Returns the HTML that displays your plugin’s settings.
 *
 * @return mixed
 */
public function getSettingsHtml()
{
   return craft()->templates->render('mailchimped/settings', array(
       'settings' => $this->getSettings()
   ));
}

And your template
{% import "_includes/forms" as forms %}

{{ forms.textField({
    label: 'Some Setting',
    instructions: 'Enter some setting here.',
    id: 'apiKey',
    name: 'apiKey', 
    value: settings['apiKey']})
}}

{{ forms.textField({
    label: 'Some Setting',
    instructions: 'Enter some setting here.',
    id: 'listId',
    name: 'listId', 
    value: settings['listId']})
}}

Yeah, thats finally it... you can access those with from everywhere
MailchimpedPlugin::getSettings()['listId']

Or
$settings = MailchimpedPlugin::getSettings();
$data = $settings->getAttributes();

> **Comments:**
> **username_0**: Ill check this out tomorrow, and mark it as the solution if it works for me. Thanks for taking the time.  
> **username_0**: Thanks a lot man. This is a really great answer. This has been super helpful, it works. Ill prob be posting a few more questions lol. Cheers.  
> **username_1**: Feel free to ask whatever you like. Craft has a steep learning curve but once you get it you'll be able to create plugins and pages in no time. We tried many systems for about 1.5 years and none of them gave the opportunity to be as fast as with craft + yii.  
> **username_0**: I agree. Its great.  

---

