1

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:

<?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()->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 %}

<div>
  {{ 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
  }) }}

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

</div>

Craft plugin

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.

2

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();
  • Ill check this out tomorrow, and mark it as the solution if it works for me. Thanks for taking the time. – Dan Zuzevich Dec 15 '17 at 2:29
  • 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 Dec 15 '17 at 16:10
  • 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 Dec 15 '17 at 16:41
  • I agree. Its great. – Dan Zuzevich Dec 15 '17 at 16: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.