1

I have another plugin development question.

I am now trying to learn how to work with the database. To that extent, I have the following code:

On the templates/settings.html page I have the following code:

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

{{ forms.radioGroup({
    label: "Radio Group Label"|t,
    instructions: "Radio Group Instructions"|t,
    name: 'radioGroupName',
    options:{ "option_1" : "option_1", "option_2" : "option_2"},
    value: settings.radioGroupName
}) }}

On the records/PluginNameRecord.php page, I have the following code:

<?php
namespace Craft;

class PluginNameRecord extends BaseRecord
{
    public function getTableName()
    {
        return 'pluginname';
    }


    public function defineAttributes()
    {
        return array(
            'radioGroupName' => array(AttributeType::String, 'default' => 'option_1')
        );
    }

}

And here is the code for the models/PluginNameModel.php file (I don't know if this is relevant or not, but I'm including it just in case):

<?php
namespace Craft;

class PluginNameModel extends BaseModel
{

    public function defineAttributes()
    {
        return array(
            'radioGroupName' => array(AttributeType::String, 'default' => 'option_1')
        );
    }
}

Now, I have tested this and it creates a table and column in mysql. However, I cannot update the data in the database. That is to say, if I select option_2 nothing happens in mysql. The same default value of option_1 still shows up.

Can someone please let me know what I am doing wrong?

Thanks,

Moshe

1 Answer 1

5

You don't need to create Records, Models or interact with the database to store your plugin settings – Craft handles this for you out of the box. Here's how:

In your main plugin file (PluginNamePlugin.php):

protected function defineSettings()
{
    return array(
        'radioGroupName' => array(AttributeType::String, 'default' => 'option_1')
    );
}

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

At a glance, your settings template looks fine, and should be placed within the pluginname/templates/ directory (filename settings.html).

If you want to retrieve a setting somewhere else within your plugin's main class, you can do it like this:

$settings = $this->getSettings();
$radioGroupName = $settings->radioGroupName;

To read the settings outside the main plugin class, you'll first need to pull the plugin instance, like this:

$plugin = craft()->plugins->getPlugin('pluginName');
$settings = $plugin->getSettings();
...

If you want to retrieve your setting(s) for use in templates, you'll need to create a template Variable class for your plugin in order to expose the data. Here's a quick example for your use case (the following would go into the file pluginname/variables/PluginNameVariable.php:

class PluginNameVariable
{
    public function settings()
    {
        $plugin = craft()->plugins->getPlugin('pluginName');
        return $plugin->getSettings();
    }
}

...and in your template:

{% set settings = craft.pluginName.settings %}
{{ settings.radioGroupName }}

For more info on plugin settings, see the official docs on the subject: http://buildwithcraft.com/docs/plugins/plugin-settings

5
  • Thanks for your response. My ultimate goal is to use this information in a template file -- does this mean that I do NOT need to save the information to the database in order to do that? If so, when does one need to set up models, records and services to interact with the database?
    – Moshe
    Jun 19, 2015 at 9:41
  • If you want to retrieve a setting for use in a template, you'll need to create a Template Variable class for your plugin. I can edit my answer to include some info on that. Jun 19, 2015 at 9:43
  • Your second question is a bit too open-ended to answer here (you might want to create separate thread(s) for any questions you might have regarding plugins and the database). Basically though, if you need to store any data beyond simple settings you'll need to look into Records, Models and Services (and more often than not, Controllers as well). I'd also suggest you take a look at the official plugin docs if you haven't already – they provide a good overview of how plugins are built/structured. Jun 19, 2015 at 9:54
  • Thanks for the help. I got it working :). I have been checking the plugin docs, but I have been having a hard time understanding what I'm suppose to do from them. Your answer helped set me in the right direction.
    – Moshe
    Jun 19, 2015 at 11:35
  • That's good to hear @Moshe – glad I could help out. Jun 19, 2015 at 11:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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